A
->static block of a class is nothing but its static initialize.
->static block of a class is nothing but its static initialize.
class b
{
int a; //instance variable
public static void main(String args[])
{
System.out.println(a);
}
}
->Compilation error raised
->instance variable can’t referred by a static method main()
->instance variable can’t referred by a static method main()
->we can’t add “static” inside static block
class b
{
static int a;
static
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("i am later");
}
}
Output:
0
I am later
->firstly for static variable memory is allocated
->secondary static block is executed
->default value is printed 0
->later main () is called
->I am later is printed
->as soon as the class is loaded into memory (secondary to primary).JVM allocates memory for static variable and given them default values after that static block is executed and the main () is executed.
Comments
Post a Comment