Example:
class Hello { int a = 99; static int b = 88; Hello(){ System.out.println("Hello D.C"); } { System.out.println("Hello I.B"); } static{ System.out.println("Hello S.B"); } public static void main(String[] args) { Hello h = new Hello(); } }
Here is what happens when the above class is compiled and run
- Allocates 8 bytes of memory for the reference variable (h) and initializes with null value.
- Verifies whether the class is loaded or not.
- If not loaded, then loads the class by doing the following tasks:
- Reads the byte code from .class file and loads into main memory.
- Allocates memory for static variables and initializes with default or specified values.
- Executes static blocks.
- Constructors will be invoked. Statement of the constructor will not be executed.
- Allocates the memory for instance variables of the class and initializes with default or specified values.
- Instance block will be executed.
- Statements from the constructor will be executed.
- Address of newly created object will be assigned to the reference variable.
No comments:
Post a Comment