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-
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,
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.
0 comments:
Post a Comment