JAVA 대상 의 초기 화 과정

2540 단어 자바Blog
http://blog.mcuol.com/User/mcuooo/Article/497_1.htm

public class Employee {
	public static String company = "A Company";
	private String name;
	private String phone = "0571-12345678";
	
	//     
	static {
		System.out.println("static code block!");
		System.out.println("Company Name :" + company +"
"); } // { System.out.println("code block"); System.out.println("company phone :" + phone+"
"); } // public Employee(){ this("string"); System.out.println("Employee()"+"
"); } // public Employee(String name){ System.out.println("Employee(String)"+"
"); this.name = name; } public static void main(String args[]){ new Employee(); } }

출력:
static code block! Company Name :A Company code block company phone :0571-12345678 Employee(String) Employee()


public class Manager extends Employee{

	public static String department = "Technology";
	private int salary = 5000;
	
	//     
	static {
		System.out.println("sub static code block");
		System.out.println("Manager department :" + department + "
"); } // { System.out.println("sub code block"); System.out.println("Manager salary :" + salary + "
"); } // public Manager(){ this("string"); System.out.println("Manager()"+"
"); } // public Manager(String name){ System.out.println("Manager(String)"+"
"); } public static void main(String args[]){ new Manager(); } }

출력:
static code block! Company Name :A Company sub static code block Manager department :Technology code block company phone :0571-12345678 Employee(String) Employee() sub code block Manager salary :5000 Manager(String) Manager()
소결:
1: 정적 변수 가 메모리 공간 을 할당 하고 초기 화
2: 대상 이 메모리 공간 을 할당 하고 기본 초기 화 합 니 다.
3: 비 정적 방법 블록 과 구조 함 수 를 초기 화하 고 실행 하 는 것 을 표시 합 니 다.
(변수 성명 에서 도 클래스 의 정적 변수 에 메모리 공간 을 할당 하고 초기 화 하 며 정적 코드 를 실행 합 니 다)

좋은 웹페이지 즐겨찾기