In this tutorial, we are going to discuss the Types of variables in Java.

Types of Variables in Java:

In Java, Variables are divided into three categories namely:

  1. Instance variables
  2. Static variables
  3. Local variables
Variables in Java

1. Instance Variables

In this type, the value of the variable is bounded to a particular instance. This value can be varied from one object to another.

Instance variables are allocated to the heap memory. Since the instance variables are allocated to memory heap, for every object a separate copy of instance variable will be created in the memory.

Instance variables are allocated to the memory at the time of creating the objects only (Runtime), hence these variables are also called as object-level variables.

When we destroy the objects, instance variables will also get destroyed.

Default values of instance variables :

For instance variables, it is not required to perform initialization explicitly. The JVM will provide default values to it.

  • For primitives, the default value is zero (0).
  • For objects, the default value is ‘null
  • For boolean, the default value is ‘false‘.

Java Instance Variables Example :

InstanceDemo.java
public class InstanceDemo {
    public static void main(String[] args) {
        Student s= new Student("chandrashekhar",37);
        System.out.println("name : "+s.name+"  roll Number: "+s.rool+" is Pass : "+s.isPass);
        Student s2= new Student("raju",4);
        System.out.println("name : "+s2.name+"  roll Number: "+s2.rool+" is Pass : "+s2.isPass);
    }
}

class Student {
    public Student(String name, int rool) {
        this.name = name;
        this.rool = rool;
    }
    public void hello(){
        int t = 10;
    }
    String name;
    int rool;
    boolean isPass;
}

Output:

name : chandrashekhar  roll Number: 37 is Pass : false                                                                        
name : raju  roll Number: 4 is Pass : false

2. Java Static Variables:

In Java static is a keyword. We can use the static keyword in front of variables, methods, blocks and classes.

Static variables are considered to be class-level variables because the values of the static variables are at class level, and not bound to be a specific instance of that class, whereas in instance variables, as mentioned earlier, the value of a variable is bound to be with a specific instance.

Where do we use?

  • If the value of a variable is not varied from one object to another object, then it is never recommended to declare that variable as a instance variable.
  • That should be declared as static. Static variables are allocated to memory in the static pool, and it is happening while loading the class itself.
  • Since the static variables are class-level variables, we don’t need any instance to access these variables.
  • We can directly access the static variables by using the class name itself.

Example for Java Static variables :

StaticVariableDemo.java
public class StaticVariableDemo {
    public static void main(String[] args) {
        int a = 0;
        Students.setCollegeName("Gayatri");
        Students students = new Students();
        students.setId(20);
        students.setStandard(12);
        students.setStudentName("chandra shekhar");
        System.out.println("students : " + students.toString() + " College Name : " + Students.getCollegeName());
        Students.setCollegeName("Geetam");
        Students students2 = new Students();
        students2.setId(21);
        students2.setStandard(13);
        students2.setStudentName("Raju");
        System.out.println("students2 : " + students2.toString() + " College Name : " + Students.getCollegeName());
        Students students3 = new Students();
        students3.setId(22);
        students3.setStandard(14);
        students3.setStudentName("Rajesh");
        System.out.println("students3 : " + students3.toString() + " College Name : " + Students.getCollegeName());
        System.out.println("students : " + students.toString() + " College Name : " + Students.getCollegeName());
    }
}
class Students {
    static String collegeName;
    String studentName;
    int id;
    int standard;
    public static String getCollegeName() {
        return collegeName;
    }
    public static void setCollegeName(String collegeName) {
        Students.collegeName = collegeName;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getStandard() {
        return standard;
    }
    public void setStandard(int standard) {
        this.standard = standard;
    }
    @Override
    public String toString() {
        return "Students{" + "studentName=" + studentName + ", id=" + id + ", standard=" + standard + '}';
    }
}

Output:

students : Students{studentName=chandra shekhar, id=20, standard=12} College Name : Gayatri
students2 : Students{studentName=Raju, id=21, standard=13} College Name : Geetam
students3 : Students{studentName=Rajesh, id=22, standard=14} College Name : Geetam
students : Students{studentName=chandra shekhar, id=20, standard=12} College Name : Geetam

Do you Know?

  • Static variables can be accessed either by using the class name or by using object reference.
  • We can access the static variables directly within the same class even if it is not required to use the class name to access them.

3. Java Local Variables :

A variable that is declared inside a method, block or a constructor is called a local variable.

Local variables are allocated to stack memory, hence these are also called as Stack variables or Temporary variables. These will be destroyed from the stack during the completion of the method execution.

Hence the scope of the local variables is as same as the scope of method or block in which it is declared.

Unlike instance variables, for local variables JVM will not provide any default values, we should give the values for the local variables before using it.

Do you know?

  1. The only applicable modifier for the local variable is final.
  2. We can’t apply public, private, protected and static modifiers for local variables.

Happy Learning 🙂