VARIABLES SIVA RAMA PRASAD KOLLU
Variables:
A variable is a storage location and has an associated type, sometimes called its compile-time
type.
Variables are divided into two types that is either a primitive type or a reference type.
A variable of a primitive type always holds a primitive value of that exact primitive type.
Ex:
int a=20;
A variable of a class type T can hold a null reference or a reference to an instance of class T
or of any class that is a subclass of T.
Ex:
Thread t=new Thread ();
Based on the behavior and position of declaration, all variables are divided into the following
three types.
1. Instance variables
2. Static variables
3. Local variables
1. Instance variables:
If the value of a variable varies from object to object, such type of variables are called instance
variables.
A separate copy of instance variables will be created for each object.
Instance variables are created upon object instantiation and destroyed upon object destruction,
thus the scope of instance variables is same as objects.
Instance variables are stored on the heap as part of the object.
Instance variables should be declared within the class directly but outside of any method,
block, or constructor.
Instance variables can be accessed directly from the instance area but cannot be accessed
directly from the static area.
However, instance variables can be accessed from the static area using object references.
Ex:
public class Main
{
int i=10;
public static void main(String[] args)
{
//[Link](i);
//C.E:non-static variable i cannot be referenced from a static context(invalid)
Main x=new Main();
[Link](x.i);//10
[Link]();
}
public void methodOne()
VARIABLES SIVA RAMA PRASAD KOLLU
{
[Link](i);//10(valid)
}
}
For instance variables, it is not required to perform initialization as the JVM will always
provide default values.
Ex:
class Test
{
boolean b;
public static void main(String[] args)
{
Test t=new Test();
[Link](t.b);//false
}
}
Note: Instance variables are also referred to as object-level variables or attributes.
2. Static variables:
If a variable's value remains constant across objects, it is not recommended to declare it
as an instance variable; rather, such variables should be declared at the class level using
the static modifier.
In contrast to instance variables, where each object has its own copy, static variables have
only one copy for the entire class, shared by all objects of the class.
Ex:
public class Main
{
int x=10;
static int y=20;
public static void main(String[] args)
{
Main a=new Main();
a.x=100;
a.y=200;
Main b=new Main();
[Link](b.x+","+b.y);//10,200
}
}
VARIABLES SIVA RAMA PRASAD KOLLU
Static variables are created at the time of class loading and destroyed at the time of class
unloading, thus the scope of static variables is same as the scope of .class file.
Process of executing a Java program:
1. Start JVM.
2. Create and start Main Thread by JVM.
3. Locate(find) [Link] by main Thread.
4. Load [Link] by main Thread. // static variable creation
5. Execution of main() method.
6. Unload [Link] // static variable destruction
7. Terminate main Thread.
8. Shutdown JVM.
Static variables are stored in the method area and should be declared directly within the
class but outside of any method, block, or constructor.
Static variables can be accessed directly from both instance and static areas.
Access to static variables can be achieved either by using the class name or by object
reference, though using the class name is recommended.
Ex:
class Main
{
static int i=10;
public static void main(String[] args)
{
Main x=new Main();
[Link](x.i);//10
[Link](Main.i);//10
[Link](i);//10
}
}
Within the same class, it is not necessary to use the class name when accessing static
variables; they can be accessed directly.
For static variables, explicit initialization is not required as the JVM always provides
default values.
VARIABLES SIVA RAMA PRASAD KOLLU
Ex:
class Main
{
static String s;
public static void main(String[] args)
{
[Link](s);//null
}
}
Note: Static variables are also referred to as class level variables or fields.
3. Local Variables:
Some times, to meet temporary requirements, programmers may declare variables inside a
method, block, or constructor; such variables are referred to as local variables or automatic
variables or temporary variables or stack variables.
Local variables are stored within the stack memory.
These variables are created as part of the execution of the block in which they are declared
and are destroyed once that block's execution completes. Therefore, the scope of local
variables is exactly the same as the scope of the block in which they are declared.
Ex:
class Test
{
public static void main(String[] args)
{
int i=0;
for(int j=0;j<3;j++)
{
i=i+j;
}
[Link](i+","+j);
}
}
C.E:cannot find symbol
[Link](i+" "+j);
symbol: variable j
location: class Main
VARIABLES SIVA RAMA PRASAD KOLLU
Ex:
public class Main
{
public static void main(String[] args)
{
try
{
int a=10/0;
}
catch(ArithmeticException e)
{
[Link](a);
}
}
}
C.E: cannot find symbol
symbol: variable a
location: class Main
The local variables will be stored on the stack.
For local variables, the JVM won't provide any default values; it is compulsory to perform
initialization explicitly before using that variable.
Ex:
public class Main
{
public static void main(String[] args)
{
int a;
[Link](a);// C.E: variable a might not have been initialized
}
}
It is never recommended to perform initialization for local variables inside logical blocks
because there is no guarantee of executing that block always at runtime.
Ex:
public class Main
{
public static void main(String[] args){
int a=10,b=20,c;
if(a>b){
[Link](c);
// C.E: variable c might not have been initialized
}
}
}
VARIABLES SIVA RAMA PRASAD KOLLU
It is highly recommended to perform initialization for local variables at the time of
declaration, at least with default values.
The only applicable modifier for local variables is 'final'. Using any other modifier will
result in a compile-time error.
Ex:
class Main
{
public static void main(String[] args)
{
public int x=10; //(invalid)
private int x=10; //(invalid)
protected int x=10; //(invalid) C.E: illegal start of expression
static int x=10; //(invalid)
volatile int x=10; //(invalid)
transient int x=10; //(invalid)
final int x=10;//(valid)
}
}
Every variable in Java should be either instance, static, or local. Additionally, every
variable in Java should be either primitive or reference. Hence, the following are the
various possible combinations for variables.
Un-initialized arrays:
Instance Level:
VARIABLES SIVA RAMA PRASAD KOLLU
Static Level:
Local Level:
Note: Once we created an array every element is always initialized with default values
irrespective of whether it is static or instance or local array.