Static Variable | Local Variable


Topics
Static Variable
  • How to declare static variable
  • When static variable will be created
  • Class loading & Unloading
  • Access static variable
Local Variable
  • When local variable will be created
Conclusions
STATIC VARIABLE

If the value of a variable is not varied from object to object , then it is not recommended to declare variable as instance variable, we have to declare such type of variables at class level by using static modifier.  

In the case of instance variable , for every object a separate copy will be created, but in the case of static variables a single copy will be created at class level and shared by every object of the class.

WHERE TO DECLARE STATIC VARIABLE: should be declared within the class directly , but outside of any block , method or constructor.

WHEN STATIC VARIABLE WILL BE CREATED : at the time of class loading. and will be destroyed at the time of class unloading. Hence, the scope of static variable is same as scope of .class file.

CLASS LOADING AND UNLOADING
Java Test
-- Start JVM
-- Create and start main Thread
-- Locate Test.class file
-- if(Test.class not found) -- Exception in main thread
-- Load Test.class  -- Static variable creation
-- Execute main method.
-- Unload Test.class  -- Static variables destruction
-- Terminate main thread
-- Shutdown JVM

Static variable will be stored in Method Area.


HOW TO ACCESS STATIC VARIABLE ? 
class Test{
static int x  = 10;
public static void main(String[] args){
Test t = new Test();
System.out.println(t.x); 10
System.out.println(Test.x); 10
System.out.println(x); 10
}
public void m(){
System.out.println(x); 10
}
}

We can access static variables  wither by object reference or class name but recommended to use class name.
Within the same class it is not required to use class name. and we can access directly.
We can access static variable directly from both instance and static areas.

For static variables JVM will provide default   values and we are not required to perform initialization explicitly.
Static variables are also known as Class level variable or Fields.

class Test{
static int x = 10;
int y = 20;
  public static void main(String [] args){
  Test t1 = new Test();
  t1.x = 888;
  t1.y = 999;
  Test t2 = new Test();
  t2.y; // 20
  t2.x; // 888
  }
}


LOCAL VARIABLE

Sometimes to meet temporary requirements of the programmer we can declare variables inside a method  or block or constructor, Such type of variables are called local variables or temporary variables or stack variables or automatic variables.

Local variables will be stored inside stack memory.

WHEN LOCAL VARIABLE WILL BE CREATED : local variables will be created while executing the block in which we declared it. Once block execution completes automatically local variable will be destroyed. Hence the scope of local variable is the block in which we declared it.

For local variable  JVM won't provide default values. We need to perform initialization explicitly before using that variable i.e if we are not using then it is not required to perform initialisation.

class Test{
public static void main(String [] args){
int x;  
System.out.println("Helllo");    
}
}

o/p : Helllo

class Test{
public static void main(String [] args){
int x;  
System.out.println(x);    
}
}

o/p : CE: variable x might not have been initilised.

class Test{
public static void main(String [] args){
int x;  
if(args.length > 10){
x = 10;

}
System.out.println(x); CE: CE: variable x might not have been initialized.
}
}


class Test{
public static void main(String [] args){
int x;  
if(args.length > 10){
x = 10;

}
else{
x = 20;
}
System.out.println(x);
}
}

Java Test A B C :  10
Java Test : 20

* It is not recommended to perform initialization for local variables inside logical blocks, because there is no guarantee for the execution of these blocks always at runtime.   

* It is highly recommended to perform initialisation for local variables at the time of declaration atleast with default values.

class Test{
int x = 10;
static int y = 20;
public static void main(String [] args){
int z = 30;

}
}

* The only applicabe modifier for local variables is final, by mistake if we are trying to applying any other modifier then we will get compile time error.

* If we are not declaring with any modifier then by default it is default, but this rule is applicable only for instance and static variables but not for local variables.

CONCLUSIONS:
For instance and static variables JVM will provide default values and we are not required to perform initialization explicitly but for local variables JVM won't provide default values , we should perform initialization explicitly before its use.

Instance and static variable can be accessed by multiple threads simultaneously  and hence these are not thread safe, but in case of local variables for every thread seperate copy will be createdd and hence local variables are thread safe.


Comments

Popular posts from this blog

Variables (Part-1)

length vs length()

Arrays in Java