A variables can be defined as to reserve memory space to store the values. These can be initialized at the time of declaration or later on depending on the type of variable. There are basically three type of variable available in java: local variables, instance variables and static variables.
Table of Contents
Types Of Variables in JAVA:
There are 3 types of variables in JAVA:
1. Local Variable
2. Instance variable
3. Class Variable/Static variable
Local variable:
- These are also called as stack variable. Because they exist in stack memory
- It is mandatory to initialize the local variable. Otherwise you will get run time error from compiler
- These can be defined inside method, constructor or also inside block. The scope or life time of local variable destroyed with end of method completion.
Instance variable:
- Instance variable are also known as member variable or field
- These are associated with the object creation. As the object get created instance variable also get created
- These live on heap memory. In case, if you don’t initialize instance variable with initial value these get default value at run time implicitly.
Class Variable/Static Variable:
- These are loaded and initialized when class is loaded in JVM
- There exists only one copy of class variable
- They live on heap memory. If these variables are not initialized to some default value is assigned to them implicitly.
Examples Of Variables In JAVA:
Given three type of variable are explained in the program written here. Simply to check the output from a java program you need JDK (java development kit) and editor to write the program (e.g. notepad, eclipse, netbeans etc).
Here, in this concept the Eclipse editor is used to check the output for different type of variables.
Open Eclipse editor, create new project and create class name TypeOfVariable and program file will be automatically save in TypeOfVariable.java. This is because when you write code in java. It is mandatory to save file name as name of class having main function defined with .java extension.
public class TypeOfVariable{ public static int staticvariable; int instancevariable; public void printValue(){ int localvariable; System.out.println("the value of staticvariable \t"+staticvariable); System.out.println("the value of instancevariable\t"+instancevariable); System.out.println("the value of localvariable \t"+localvariable); } public static void main(String args[]){ TypeOfVariable object=new TypeOfVariable(); object.printValue(); } }
Here, in this program three variables named as staticvariable, instancevariable and localvariable are declared as respective for static variable, instance variable and local variable.
Important Note: If you run this program without initializing localvariable you will get an compile time error. This is simple because local variable must be initialized otherwise it will show an error like “The local variable localvariable may not have been initialized”.
Correct Program Code With Initialization of Local Variable:
Simply just assign the initial value to localvariable in the same program. The change in output will reflect automatically the importance of this change. Because only local variable required to initialize with some value.
Class/Static and instance variable implicitly assign the null /0/ false value to these particular variable.
public class TypeOfVariable{ public static int staticvariable; int instancevariable; public void printValue(){ int localvariable = 10; System.out.println("the value of staticvariable \t"+staticvariable); System.out.println("the value of instancevariable\t"+instancevariable); System.out.println("the value of localvariable \t"+localvariable); } public static void main(String args[]){ TypeOfVariable object=new TypeOfVariable(); object.printValue(); } }
Our Output for above program is given here:
the value of staticvariable 0 the value of instancevariable 0 the value of localvariable 10