Variable Types in Java
There are three types of variable in java
- Local variable.
- Instance variable.
- Static variable.
Local variable
- A variable that is declared inside the method is called local variable.
- A local variable in Java is a variable that's declared within the body of a method. Then you can use the variable only within that method.
- Local variables are declared in methods, constructors, or blocks.
- Local variable can't be declared as static. Two local variables can not have same name if there scopes are same.
- Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
- Access modifiers cannot be used for local variables.
- Local variables are visible only within the declared block.
- There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
Example :
class Local
{
public static void main(String args[])
{
int i=10;
int j;
System.out.print(j);//Variable j might not have been initialize
for(int i=1;i<=5;i++)
{
System.out.print(i);//error of duplicate variable
}
System.out.print(i);
}
}
Instance Variable
- A variable that is declared inside the class but outside a method, constructor or any block is called instance variable. It is not declared as static.
- These variables are declared at the top level. They begins their life when first class loaded into memory and ends when class is unloaded.
- There will be as many copies of the instance member variables as there are number of objects(one copy for each object).
- Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
- These variables will get memory as soon as we declared an object and inside the object.
- new operator provides memory to these variables.
- Instance variables can be declared in class level before or after use.
- Access modifiers can be given for instance variables.
- Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.
- Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class ( when instance variables are given accessibility) should be called using the fully qualified name . ObjectReference.VariableName.
Static variable
- Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. It cannot be local.
- There will be only one copy of the static member variable irrespective of number of objects and this copy will be shared by all the objects.
- Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value.
- Static variables are created when the program starts and destroyed when the program stops.
- Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks.
- These variable will get memory as soon as Jvm loads the class in memory, before creating any object.
- Static is a keyword.
- These variable can be accessed through class name as well as object name.
- Static variables are initialized when class is loaded.
- Static variables in a class are initialized before any static method of the class runs.
- These variables are having highest scope that is they can be accessed from any method/ block in a class.
- When declaring class variables as public static final, then variables names (constants) are all in upper case.
Example :
class A
{
int x; //instance variable
static int y;//static or class variable
}
class Variable
{
public static void main(String args[])
{
int n;//local variable
System.out.println(Test.y);
A a1=new A();
A a2=new A();
A a3=new A();
a1.x=5;
a1.y=10;
a2.x=6;
a2.y=11;
a3.x=7;
a3.y=12;
System.out.println(a1.x+","+a1.y);
System.out.println(a2.x+","+a2.y);
System.out.println(a3.x+","+a3.y);
}
}
output :