inside the java virtual machine 再读

The lifetime of a class and an object

Initialization of a class consists of two steps:

  • init its direct super class, it its direct super class hasn't already inited.
  • executing the class's class init method, if it has one.

The JVM initializes types on their first active use, Only four activities constitute an active use:

  • invoking a class initialization method on a new instance of a class,
  • creating an array whose element type is the class,
  • invoking a method declared in a class,
  • and accessing a non-constant field declared in a class or interface. A use of a non-constant field is an active use of only the class or interface that actually declares the field.

 

When the Java Virtual Machine creates a new instance of a class, either implicitly or explicitly, it first allocates memory on the heap to hold the objectís instance variables. Memory is allocated for all variables declared in the object's class and in all its superclasses, including instance variables that are hidden, a pointer to class data in the method area, are also likely allocated at this point. As soon as the virtual machine has set aside the heap memory for a new object, it immediately initializes the instance variables to default initial values.

The Java compiler generates at least one instance initialization method for every class it compiles. In the Java class file, the instance initialization method is named "<init." 

the <init() method will have three components:

  • an invocation of a superclass <init() method
  • the bytecodes for any instance variable initializers
  • the bytecodes that implement the body of the corresponding constructor

 

posted on 2012-03-15 09:52  grep  阅读(410)  评论(0编辑  收藏  举报