Variables in Java
A variable is the name given to a memory location. It is the basic unit of storage in a program.
- The value stored in a variable can be changed during program execution.
- A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
- In Java, all the variables must be declared before use.
How to declare variables?
We can declare variables in java as follows:

datatype: Type of data that can be stored in this variable.
variable_name: Name given to the variable.
value: It is the initial value stored in the variable.
Examples:
float simpleInterest; //Declaring float variable int time = 10, speed = 20; //Declaring and Initializing integer variable char var = 'h'; // Declaring and Initializing character variable
Types of variables
There are three types of variables in Java:
- Local Variables
- Instance Variables
- Static Variables
Let us now learn about each one of these variables in detail.
-
Local Variables: A variable defined within a block or method or constructor is called local variable.
- These variable are created when the block in entered or the function is called and destroyed after exiting from the block or when the call returns from the function.
- The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variable only within that block.
- Initilisation of Local Variable is Mandatory.
Sample Program 1:
publicclassStudentDetails{publicvoidStudentAge(){//local variable ageintage =0;age = age +5;System.out.println("Student age is : "+ age);}publicstaticvoidmain(String args[]){StudentDetails obj =newStudentDetails();obj.StudentAge();}}chevron_rightfilter_noneOutput:
Student age is : 5
In the above program the variable age is local variable to the function StudentAge(). If we use the variable age outside StudentAge() function, the compiler will produce an error as shown in below program.
Sample Program 2:
publicclassStudentDetails{publicvoidStudentAge(){//local variable ageintage =0;age = age +5;}publicstaticvoidmain(String args[]){//using local variable age outside it's scopeSystem.out.println("Student age is : "+ age);}}chevron_rightfilter_noneOutput:
error: cannot find symbol " + age);
-
Instance Variables: Instance variables are non-static variables and are declared in a class outside any method, constructor or block.
- As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
- Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
- Initilisation of Instance Variable is not Mandatory. Its default value is 0
- Instance Variable can be accessed only by creating objects.
Sample Program:
importjava.io.*;classMarks{//These variables are instance variables.//These variables are in a class and are not inside any functionintengMarks;intmathsMarks;intphyMarks;}classMarksDemo{publicstaticvoidmain(String args[]){//first objectMarks obj1 =newMarks();obj1.engMarks =50;obj1.mathsMarks =80;obj1.phyMarks =90;//second objectMarks obj2 =newMarks();obj2.engMarks =80;obj2.mathsMarks =60;obj2.phyMarks =85;//displaying marks for first objectSystem.out.println("Marks for first object:");System.out.println(obj1.engMarks);System.out.println(obj1.mathsMarks);System.out.println(obj1.phyMarks);//displaying marks for second objectSystem.out.println("Marks for second object:");System.out.println(obj2.engMarks);System.out.println(obj2.mathsMarks);System.out.println(obj2.phyMarks);}}chevron_rightfilter_noneOutput:
Marks for first object: 50 80 90 Marks for second object: 80 60 85
As you can see in the above program the variables, engMarks , mathsMarks , phyMarksare instance variables. In case we have multiple objects as in the above program, each object will have its own copies of instance variables. It is clear from the above output that each object will have its own copy of instance variable.
-
Static Variables: Static variables are also known as Class variables.
- These variables are declared similarly as instance variables, the difference is that static variables are declared using the static keyword within a class outside any method constructor or block.
- Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.
- Static variables are created at start of program execution and destroyed automatically when execution ends.
- Initilisation of Static Variable is not Mandatory. Its default value is 0
- If we access the static variable like Instance variable (through object), compiler will show the warning message and it wont hault the program. Compiler will replace the object name to class name automatically.
- If we access the static variable without classname, Compiler will automatically append the class name.
To access static variables, we need not to create any object of that class, we can simply access the variable as:
class_name.variable_name;
Sample Program:
importjava.io.*;classEmp {// static variable salarypublicstaticdoublesalary;publicstaticString name ="Harsh";}publicclassEmpDemo{publicstaticvoidmain(String args[]) {//accessing static variable without objectEmp.salary =1000;System.out.println(Emp.name +"'s average salary:"+ Emp.salary);}}chevron_rightfilter_noneoutput:
Harsh's average salary:1000.0
Instance variable Vs Static variable
- Each object will have its own copy of instance variable whereas We can only have one copy of a static variable per class irrespective of how many objects we create.
- Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of instance variable. In case of static, changes will be reflected in other objects as static variables are common to all object of a class.
- We can access instance variables through object references and Static Variables can be accessed directly using class name.
- Syntax for static and instance variables:
class Example { static int a; //static variable int b; //instance variable }
Similar Articles:
- Scope of Variables in Java
- Comparison of static keyword in C++ and Java
- Are static local variables allowed in Java?
- Instance Variable Hiding in Java
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Scope of Variables In Java
- Using Variables in JShell of Java 9
- final variables in Java
- Final local variables in Java
- Are static local variables allowed in Java?
- Swap two variables in one line in C/C++, Python, PHP and Java
- Assigning values to static final variables in Java
- Static and non static blank final variables in Java
- Difference between static and non-static variables in Java
- Variables and Keywords in C
- Global and Local Variables in Python
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- How to assign values to variables in Python and other languages
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java



