Wednesday, 28 August 2013

SIB

it stands for Static initialization Block.it comes under initializer and before main method all initializer would executing.so first SIB executing then main method no matter SIB is present after or before main method...

Example:-


first SIB executes then from main...

SIB executes only one time throughout the whole execution .it is not dependent on the object creation.

Example:-


in this we create the object of hello class 2 times but it calls SIB only once........

We cannot declare local variable as a static.
We cannot use static keyword in the SIB .if we do this it will give compile time error.
like this


=> if we call any method from main and SIB is present anywhere in the program then first SIB prints then method and after this we will get main method....

Example:-


initialization through SIB

Example:-


initialization occurs from top to bottom....
first it executes all initializer after this it executes the main method.....

in the above program we initialize i before declaration it will not give any error we can do this because all initializers executes  before the main......

we cannot use non static member in the SIB


No comments :

Post a Comment