public class House {
static int numOfPillars = 4;
private int numOfMembers = 6;
private String nameOfHouse = "The Hom's family";
public House() {
System.out.println(this.getClass().getSimpleName() + " constructor called");
}
}
static fields are initialised when the class is first loaded.
1. House.<clinit>() method is called, initialise static fields, in this case: numOfPillars;
2. House.<init>() method is called, initialise non-static fields, such as numOfMembers, nameOfHouse;
3. and then House.<init>() calls the constructor public House(){}
Typical class initiation
----------Bytecode Level----------------
new #16 // class Subclass
dup
invokespecial #18 // Method Subclass."<init>":()V
astore_2
new - create new object of type identified by class reference in constant pool index, in this case #16
the duplicate the return value of 'objectref'
invokespecial method consumes one of the two 'objectref' in the stack, calling the <init> method of the class.
and then stores the rest 'objectref' into local variable table
No comments:
Post a Comment