Division 1:
Based on type of value represented by a variable all variable are divided into two types:
a. Primitive variables: Can be used to represent primitive values.
int x = 10;
b. Reference variable: Can be used to refer objects.
student s = new student(); (s variable refer student object)
Division 2:
Based on position of declaration and behaviour all variables are divided into three types:
a. Instance variable
b. static variable
c. local variable
Instance variable:
1. If the value of a variable is varied from object to object such type of variable are called Instance variable.
2. For every object a separate copies of instance variable will be created.
3. Instance variable should be declared within the class directly but outside of any method,constructor or block.
4. Instance variable will be created at the time of object creation and destroyed at the time of destruction.Hence,the scope of Instance variable is exactly same as the scope of objects.
5. Instance variable will be stored in the Heap memory as a part of object.
6. We cannot access Instance variable directly from static area but we can access by using object reference.
7.But we can access Instance variable directly from Instance Area.
sample program:
class Test
{
int x = 10;
public static void main(String[] args)
{
System.out.println(x); (invalid we cannot access instance variable directly into static area)
Test t = new Test();
System.out.println(t.x); o/p:10 (valid )
}
}
8.For Instance variable JVM always provide default value and we are not required to perform initilization explicitly.
sample program:
class Test
{
int x;
double d;
boolean b;
String s;
public static void main(String[] args)
{
Test t = new Test();
System.out.println(t.x); o/p:0
System.out.println(t.d); o/p:0.0
System.out.println(t.b); o/p:false
System.out.println(t.s); o/p:null
}
}
9.Instance variable also known as object level variables.
static variable:
1. If the value of 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 variable at class level by using static modifier.
2. In the case of Instance variable for every object a separate copy will be created but in the case of static variable a single copy will be created at class level and shared by every object of a class.
3. static variable should be declared within the class directly but outside of any method,constructor or block.
4. static variable will be created at the time of class loading and destroyed at the time of class unloading.Hence,the scope of static variable is exactly same as the scope of .class file.
5. static variable will be stored in method area.
6. We can access static variables either by object reference or by 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.
sample program:
class Test
{
static int x = 10;
public static void main(String[] args)
{
Test t = new Test();
System.out.println(t.x); o/p:10
System.out.println(Test.x); o/p:10
System.out.println(x); o/p:10
}
}
7. For static variable JVM always provide default value and we are not required to perform initilization explicitly.
sample program:
class Test
{
static int x;
static double d;
static boolean b;
static String s;
public static void main(String[] args)
{
System.out.println(x); o/p:0
System.out.println(d); o/p:0.0
System.out.println(b); o/p:false
System.out.println(s); o/p:null
}
}
8. static variable also known as class level variable or fields.
local variable:
1. sometime to meet temporary requirement of the programmer we can declare variables inside a method or block or constructor such type of variable are called local variables,temporary variable or stack variables or Automatic variables.
2. local variable will be stored inside stack memory.
3. local variable will be created while executing the block in which we declare it.Once block execution complete automatically local variables will be destroyed.Hence,the scope of variable is block in which we declare it.
4. for local variable JVM cannot provide default value compulsory we should perform initilization explicitly.Before using that variable i.e if you are not then it is not required to perform initilization.
5. the only applicable modifier for local variable is final by mistake if you trying to apply any other modifier then we will get compile time error.
sample program:
class Test
{
public static void main(String[] args)
{
int x = 30; (local variable inside a static block)
}
}