Java Variables
Java Variables
1. Instance variables
2. Static variables
3. Local variables
Instance variables:
If the value of a variable is varied from object to object such type of variables are
Instance variables will be created at the time of object creation and destroyed at
the time of object destruction hence the scope of instance variables is exactly
Instance variables should be declared with in the class directly but outside of any
Instance variables can be accessed directly from Instance area. But cannot be
But by using object reference we can access instance variables from static area.
Example:
class Test
int i=10;
//System.out.println(i);
context(invalid)
t.methodOne();
System.out.println(i);//10(valid)
For the instance variables it is not required to perform initialization JVM will always
Example:
class Test
boolean b;
System.out.println(t.b);//false
Static variables:
If the value of a variable is not varied from object to object such type of variables
In the case of instance variables for every object a separate copy will be created
but in the case of static variables for entire class only one copy will be created
Static variables will be crated at the time of class loading and destroyed at the
time of class unloading hence the scope of the static variable is exactly same as
with in the class directly but outside of any method or block or constructor.
Static variables can be accessed from both instance and static areas directly.
We can access static variables either by class name or by object reference but
But within the same class it is not required to use class name we can access
directly.
For the static variables it is not required to perform initialization explicitly, JVM will
Example:
class Test
static String s;
System.out.println(s);//null
Example:
class Test
int x=10;
t1.x=888;
t1.y=999;
System.out.println(t2.x+"----"+t2.y);//10----999
}
Diagram:
Local variables:
variables inside a method or block or constructors such type of variables are called local
The local variables will be created as part of the block execution in which it is declared
and destroyed once that block execution completes. Hence the scope of the local
Example 1:
class Test
int i=0;
for(int j=0;j<3;j++)
i=i+j;
}
System.out.println(i+”-----”+j); // invalid, this will give you error
}}
For the local variables JVM won't provide any default values compulsory we
runtime.
Note: The only applicable modifier for local variables is final. If we are using any other
Example:
class Test
expression
Conclusions:
1. For the static and instance variables it is not required to perform initialization
explicitly JVM will provide default values. But for the local variables JVM won't
2. For every object a separate copy of instance variable will be created whereas for
entire class a single copy of static variable will be created. For every Thread a
and hence these are not Thread safe but local variables can be accessed by only
one Thread at a time and hence local variables are Thread safe.
4. If we are not declaring any modifier explicitly then it means default modifier but
this rule is applicable only for static and instance variables but not local variable.