Blocks
- Block is a set of instructions defined in curly braces.
- Block doesn't have any name so you can't invoke the block explicitly. JVM will invoke it automatically.
- Blocks defined inside the class directly as a member are called as INITIALIZATION BLOCKS.
There are two types of initialization blocks
- Instance Initialization Block (IIB)
- Static Initialization Block (SIB)
Instance Initialization Block (IIB)
- Block defined inside the class directly without the static modifier is called as INSTANCE INITIALIZATION BLOCK.
- IIB will be invoked by the JVM automatically after initializing the object.
- JVM will invoke instance block when an object is created.
Static Initialization Block (SIB)
- Block defined inside the class directly with the static modifier is called as STATIC INITIALIZATION BLOCK.
- SIB will be invoked by the JVM automatically after initializing the class (or loading the class).
- JVM will invoke static block at the time of class loading.
- Static block will be called only once because class is loaded only once.
class Test{ int a; static int b; //Instance Initializer Block (IIB) { System.out.println("IIB"); } //Static Initializer Block (SIB) static{ System.out.println("SIB"); } }
Note: Variable (instance or static) cannot be initialized separately. It has to be either declared and initialized in the same statement. Refer below programs:
Example : 1
class Test1{ public static void main(String args[]){ Hello h = new Hello(); System.out.println(h.a); } } class Hello{ int a; a = 10; //cannot be instantiated seperately } class Test2{ public static void main(String args[]){ System.out.println(Hello.a); } } class Hi{ static int a; a=10; //cannot be instantiated seperately } //Output - C.T.E
Example : 2
class Test{ public static void main(String args[]){ System.out.println(Hello.a); System.out.println(Hello.a); } } class Hello{ static int a = 10; { System.out.println("Instance block"); } static{ System.out.println("Static block"); } } /*Output - Static block 10 10 Points to note: 1. Class is loaded when a field is accessed. In this case, Hello.a. 2. Class is loaded only once, though the static variable was called twice. */
Example : 3
class Test{ public static void main(String args[]){ Hello h = null; System.out.println("Ref is created"); } } class Hello{ static int a = 10; { System.out.println("Instance block"); } static{ System.out.println("Static block"); } } /*Output - Re is created Points to note: 1. Class is not loaded when a reference variable is created. */
Example 4:
class Test{ public static void main(String args[]){ Hello h = null; System.out.println("Ref is created"); h = new Hello(); Hello h1 = new Hello(); } } class Hello{ static int a = 10; { System.out.println("Instance block"); } static{ System.out.println("Static block"); } } /*Output - Ref is created Static block Instance block Instance block Points to note: 1. Class is not loaded when a reference variable is created. 2. Class is loaded when an object is created and is loaded only once irrespective of the number of objects created. 3. Instance block is executed every time an object is created, so it can be executed more than once. */
Example 5:
class Test{ public static void main(String args[]){ Hello h = new Hello(); System.out.println("Main : "+Hello.a); } } class Hello{ static int a; { a=10; //Static variable can be accessed directly from an instance block. System.out.println("Instance block"); } } /*Output - Main: 0 Points to note: 1. Class is loaded when a field of the class is accessed (Hello.a). 2. Instance block is not loaded as object was not created. 3. static variables are initialized to the default values by the JVM during class loading. Therefore, a = 0; */
Example 6:
class Test{ public static void main(String args[]){ System.out.println("Main : "+Hello.a); } } class Hello{ static int a; int b; static{ a=10; //Static variable can be accessed directly from an instance block. //b=20; //non-static variables cannot be accessed directly from static block. Otherwise C.T.E Hello h = new Hello(); h.b=30; //instead an instance/object can be created and non-static variable accessed System.out.println("Static block"); } } /*Output - Static block Main : 10 */
Example 7 :
class Test{ public static void main(String args[]){ System.out.println("Main in Test class"); } static{ System.out.println("Static block in Test class"); } } /* Output: Static block in Test class Main in Test class Points to note: 1. Class is loaded when the program is executed. i.e java Test */
Example 8 :
class Test{ public static void main(String args[]){ System.out.println("Main "+Hello.a); } } class Hello{ static int a; static{ System.out.println("SIB 1"); } static{ System.out.println("SIB 2"); } } /* Output: SIB 1 SIB 2 Main : 0 */
Example 9 :
class Test{ public static void main(String args[]){ Hello h = new Hello(); System.out.println("from Main "); } } class Hello{ { System.out.println("IIB 1"); } { System.out.println("IIB 2"); } } /* Output: IIB 1 IIB 2 from Main */
Different scenarios when class is loaded:
1. When a program is executed. Example: java Hello.
2. When object is created. Hello h = new Hello();
2. When object is created. Hello h = new Hello();
3. When a member of class is accessed (method or variable).
Hello.a;
Hello.method1();
Note: Class is not loaded or static blocks are not executed when a reference variable of a class is created. Only memory is allocated.
Hello h = null;
No comments:
Post a Comment