Topic outline

    • A variable is a container which holds the value while executing the Java programme. A variable with a data type is allocated. 

      The name of the memory location is a variable. In Java, there are three kinds of variables: local, instance and static. 

      In Java, there are two kinds of data types: primitive and non-primitive.

      There are three types of variables in Java:

      1. local variable

      2. instance variable

      3. static variable

      1) Local variable 

      A variable declared inside the body of the method is called a local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists.

      A local variable cannot be defined with "static" keyword.

      2) Instance Variable :

      A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static.

      It is called instance variable because its value is instance specific and is not shared among instances.

      3) Static variable :

      A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

      Following is an example to illustrate- 

      package examples;

      public class variables_example {

      public static void main(String[] args)


      String str = "";

      int number = 20;

      double decimalNum = -26.6;

      char ch = 'A';

      boolean b = true;

      float fNum = 0.66666f;

      System.out.println("Integer varible = " + number);

      System.out.println("String varible = " + str);

      System.out.println("Double varible = " + decimalNum);

      System.out.println("Character varible = " + ch);

      System.out.println("Boolean varible = " + b);

      System.out.println("Float varible = " + fNum);




      Integer varible = 20

      String varible = 

      Double varible = -26.6

      Character varible = A

      Boolean varible = true

      Float varible = 0.66666