We can categorize the types of variables in java as
Local variable
1> Local variable
2> Static variable
3> Instance variable
4> Reference variable
Static and instance variable can be of reference type.And both static and instance are the scope of class it means we are defining in the class only. We cannot define static variable in the local scope like in the method block. For details about static variable Click Here .
Local variable
Local variables are having local scope to access or we can say that they are having temporary access. A variable defined in the method or looping block are considered as a local variable. Memory for the local variable are created while invoking the method and destroyed when execution of the method block ends that means every time memory for the local variable will be creating when JVM will invoke instance / static method and will be destroyed when it finishes method block execution. If are we are defining any variable inside the looping block then its scope will remain within the looping block only. And every time memory will created and destroyed while executing and finish executing the block respectively.
Static variable
1> Static variable is the scope of class that means it is instantiated by the JVM while loading the class.
2> Most importantly static variable can be accessed without creating the object.
3> We can be able to access the static variable either with the class name or directly.
4> For static variable memory will be allocated once and will be while loading the .class file.
5> Any change in the memory location of the static content, will reflect to all the accessing object.
6> Use of static variable is when we need to assign the same content for all the objects.
7> Making use of static variable is good practice as because memory allocated once by the JVM.
8> Static variable is accessible any where in the class.
For details about Static keyword in java then just Click Here .
Instance variable
Like static variable it is also the scope of the class but it is only accessible with the object only and constructor is responsible for allocating the memory for the instance variable and object is responsible for the calling constructor.
By default its value is zero if variable is of primitive type and null if the it is of reference type.
Memory created for the instance variable is different for different object. It means if we are modifying the instance variable with one of the object then will not reflect to the other object.
We cannot access instance variable from the static content. But why ?
It is because static content can be accessible without the creation of any object but to access the instance member we need an object. That is why compiler is not very sure that developer is surely going to create for in its program and hence it will give compilation error "non-static variable x cannot be referenced from a static context" .
Possible ways to access the instance variable
1> With the reference that has the object reference value. For ex-
2> With the object directly . For ex -
Reference variable
Instance variable
Like static variable it is also the scope of the class but it is only accessible with the object only and constructor is responsible for allocating the memory for the instance variable and object is responsible for the calling constructor.
By default its value is zero if variable is of primitive type and null if the it is of reference type.
Memory created for the instance variable is different for different object. It means if we are modifying the instance variable with one of the object then will not reflect to the other object.
We cannot access instance variable from the static content. But why ?
It is because static content can be accessible without the creation of any object but to access the instance member we need an object. That is why compiler is not very sure that developer is surely going to create for in its program and hence it will give compilation error "non-static variable x cannot be referenced from a static context" .
Possible ways to access the instance variable
1> With the reference that has the object reference value. For ex-
< class_name > < var_name > = new < class_name >( ) ;
< var_name > . < instance_variable > ;
2> With the object directly . For ex -
new < class_name > . < instance_variable > ;
Reference variable
Reference variable can be both static and non-static. It is generally used to store the reference value of the object and hence points the object memory location.
Assigning null value to any reference means that now it is not assigning to any object in the heap memory.
What will happen when we will define any reference as static ?
Defining static to a variable is used for making singleton class. Singleton class means restricting user to create more than one object.
Let me make you clear with an example regarding variables
public class prog {
int x ; // instance variable
String y ; // instance reference variable
static int i ; // static primitive variable
static String j ; // static reference variable
public static void main(String sush[ ]) {
prog p = new prog() ; //Object creation
prog p1=null ; //Reference variable with null value
}
}
nice article for beginners.thank you.
ReplyDeletejava variable
java variable