Showing posts with label Core Java- Static keyword. Show all posts
Showing posts with label Core Java- Static keyword. Show all posts
Now lets have some useful stuffs regarding initialization block. It is just a block defined as the static. We can have print statement ,  static method call but we cannot declare any static variable in static initialization block. When any class is loaded then first JVM will initialize the static variable and then execution starts and initialization block statements are first  to be executed by the JVM even before executing main ( ) .

Syntax of static initialization block :
< static > {  } 

 Let me make you clear with the help of an example.

public class prog4 {

static int i ;

static {

System.out.println("Static initialization Block");

}

public static void main(String sush[]) {

System.out.println(i);

    }

}

In the above example we can see that after declaring static variable a static block is defined and then main method.

Steps for the execution of the above program : 


1 >Here first JVM will load the .class file and while loading it will first initialize the static variable. By default its value is zero.

2> After initializing static variable it will start execution and first it will execute the all the static block defined in the program.

3> After executing it will execute the main method. But if there is any function call in the static block then the control will be transferred to the static method. 

Like if we want to store the return value from a method to the static variable then it will first execute the method then assign the value to the static variable and then the execution will start with the static block if any.
For ex-

public class prog4 {

static int i=value() ;

static int value (){
return 2 ;

static {
System.out.println("Static initialization Block");
}
public static void main(String sush[]) {

System.out.println(i);
    }
}

Steps of execution for the above program :

1 >First JVM will call the value() and then assign 2 to the static variable " i ". 

2> After assigning value to the variable it will execute the static block and print the " initialization block ".

3> Then execute the main () and print the value of i as " 10 " .

Note : If we have multiple static block then execution will follow the order of declaration of static block . Execution will be top to bottom from the class declaration . And if any sub class extending super class then all super class static block will be executed first then all sub-class static block. 
Like static variable it is also the scope of class. We know that to call a method object is needed but if the method is declared as static then no need to create object for calling the method because it can be called with the class name or we can call a static method from a static or non-static method without its class name. If we try to call a instance method from a static method directly by its method name then the compiler will give compilation error saying " non-static method method_name( ) cannot be referenced from a static content " .

Why compiler gives such type of error ?
For accessing a instance method we need to create an object and must be a part of JVM to restrict calling here but compiler is restricting while compiling  because the compiler is not sure that whether you have created an object in your program or not to access the instance method. That is why it is restricting while compiling our source file. For ex-

public class prog3 {

void display( ) {

System.out.println("Instance method");

}

public static void main(String sush[ ]) {

display( );  //Calling instance method "display()" from static 
               method main()

    }

}


In the above example display() is a instance method and we are trying to call it from a static method i.e main() , hence compiler is giving error because compiler is not sure for an object creation by code developer.

Note : We can reference an static method from a static or non-static method directly because it doesn't need any object for calling. 

Important points regarding static method 

1 > Like all others static method it can have an argument,return type, modifiers.

2> Any static method cannot be override.

3> We cannot declare two static method within  a class with same name.

4> Like all other methods we can overload static method as well.

5> Cannot declare any static variable in a static or non-static method Click here for details .
Discussed in brief in its previous post now lets go in dept about static variable with some interesting programs. Default value of static variable is zero where as JVM allocate memory only once. For ex-

import java.lang.Object;

public class prog1 {
static int x;

public static void main(String sush[ ]) {

System.out.println(x) ;
System.out.println(prog1.x) ;
    }
}
In the above program we can see that " x " is a static variable and if not initialized then its default value is zero. Here we have two print statement , one is direct printing " x " value and another is through class name. Both the statement are valid and will print zero because static variable is the scope of class.

Note : No object is being created still it compile and execute successfully, it indicate static has no concern with Object creation and hence JVM will read all the static member define in the program and allocate memory for it while loading the class.

Now think about the declaration of static variable, i mean where we can be able to define the static variable.

Can we define Static variable inside the static or non-static method ?

- No, we can't . It is because any variable declared within a method is valid within the method block only, i mean within the curly braces of the method only. And we know that for a static variable memory is only once created, then what is the need of declaring static in a static or non-static method ? Since, within a method the scope of any variable is local . And hence we cannot use that variable outside the block so no need to created the memory permanent by declaring a static variable within a method till the application finishes its full execution. lets see an example regarding this,

public class prog2 {

public static void main(String sush[ ]) {

static int x;                         // Static variable

System.out.println(x);               // Compilation error

System.out.println(prog2.x);         // Compilation error

    }
}
In the above program we defined a static variable in a static method i.e main( ) and the compiler will give error saying " Illegal Start of Expression " . If we think normally then its not a error but if we concern memory then declaring static in any method is waste of memory as because it consume memory permanently till program execution which is of no use for a local scope.



Static in java can be defined in various ways according to its use, like we have a static variable , a static method and a static initialization block but the common of all above three type is that all get initialized at the time of Class Loading . Let got in dept of all these three types of static type definition in java.

Basically in java we are mainly dealing with Object and Static functionality. And both are independent of each other it means static has no concern with Object and vice-versa.

Why Static has no concern with Object ?
It is because Static is a part of class that means no need to create any Object to access any static member . It can be accessible with the class name like,

< Class_name > . < Static_member >  ; 

Why Static has introduced into java ?
It is because to work with one time memory creation through out the program. But the same is not in the case of Object, for every Object JVM is going to allocate memory for it whether it is going to be used in the program or not, that means there may be chance of wastage of memory and with the static keyword JVM is going to allocate memory for one time only at the loading time and can be modified by any number of object and hence the modified value will be updated at the memory location of static member.

Static Variable 

It is a class variable which means it can be accessible without creating any object. Its memory is once created at the class loading time and can be modified multiple times and memory remain through the program. Default value of any static variable is zero . For details on Static variable click here For Details on Static Variable click here !

Syntax for declaring Static variable :
< static >  < data_type >  < var_name >  =  < value > ;

Static method 
Like static variable we can also define any method in java as static . Declaring a method as static means it can be accessible without creating object and with the class name.

Syntax for declaring static method :
< static >  < return_type >  < method_name > ( ) { }  

for ex-
static void fun_name ( ) {
}


Static initialization block 

It is a static block and again it will be executed first of it is available in the java program because it is loading first by the JVM.

Syntax for declaring Static initialization block :
< static > {  }

for ex-
static {
System.out.println ( " Static initialization Block" ) ;

}

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic