Variables in Java

A variable is the name given to a memory location. It is the basic unit of storage in a program.

1) The value stored in a variable can be changed during program execution.

2) A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.

3) In Java, all the variables must be declared before they can be used.

How to declare variables?

Syntax declaring variables in java as follows:

 datatype  variable_name = value; 
    eg. int a=50 ;

  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..

  Memory Representation :


//Declaring float variable
float salary=45.56f; 

//Declaring double variable
float salary=4523434.6543;

//Declaring and Initializing integer variable
int time = 10 ;
int speed = 20; 

// Declaring and Initializing character variable
char var = 'h'; 

//Declring and Initialization character variable
String str="Hello java";

Types of variables

There are three types of variables in Java:

1) Local Variables

2) Instance Variables

3) Static Variables

Local Variables

A variable defined within a block or method or constructor is called local variable.

public class StudentDetails
    public void StudentAge() 
    {   //local variable age
        int age = 0;
        age = age + 5;
        System.out.println("Student age is : " + age);
    public static void main(String args[])
        StudentDetails obj = new StudentDetails();
Student age is : 5

  These variables are created when the block 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.

Instance Variables

Instance variables are non-static variables and are declared in a class outside any method, constructor or block.

//example of Instance variable
class Marks
    //These variables are instance variables.
    //These variables are in a class and are not inside any function
    int engMarks;
    int mathsMarks;
    int phyMarks;
class MarksDemo
    public static void main(String args[])
    {   //first object
        Marks obj1 = new Marks();
        obj1.engMarks = 50;
        obj1.mathsMarks = 80;
        obj1.phyMarks = 90;
        //second object
        Marks obj2 = new Marks();
        obj2.engMarks = 80;
        obj2.mathsMarks = 60;
        obj2.phyMarks = 85;
        //displaying marks for first object
        System.out.println("Marks for first object:");
        //displaying marks for second object
        System.out.println("Marks for second object:");
Marks for first object:
Marks for second object:

  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.

  A separate copy of each instance variable is created for each object .

  We may use access modifier for instance variables. If we do not specify any access modifier then the default access modifier will be used.

Static Variables

Static variables are also known as Class variables.

//example of static variables
class Emp {
   // static variable salary
   public static double salary;
   public static String name = "Durgesh";
public class EmpDemo
     public static void main(String args[]) {
      //accessing static variable without object         
      Emp.salary = 1000;
      System.out.println( + " : average salary:" + Emp.salary);
 Durgesh : average salary:1000.0

  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.

  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.

  To access static variables, we need not to create any object of that class, we can simply access the variable as:

We can also call static variable on objects.

Instance variable Vs Static variable

Instance variable Static variable
Each object will have its own copy of instance variable 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 . Static Variables can be accessed directly using class name.
woman avatar