JAVA 대상 의 초기 화 과정
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: 비 정적 방법 블록 과 구조 함 수 를 초기 화하 고 실행 하 는 것 을 표시 합 니 다.
(변수 성명 에서 도 클래스 의 정적 변수 에 메모리 공간 을 할당 하고 초기 화 하 며 정적 코드 를 실행 합 니 다)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.