What are the different types of variables in Java?

Article by: Manish Methani

Last Updated: October 27, 2021 at 2:04pm IST
2 mins 57 sec read

Like human memory, Computer memory has millions of cells where it can store data or information. Now to retrieve an information from those cells, each cell should be given one name called as 'variable name'. These variables can contain any data type from the list int, float, double, char, string.

Example :

int a, b, c;             /* Declares three ints, a, b, and c. */
int p = 10, q = 10;         /*  Example of initialisation */
byte byteVariable = 22;     /* initializes a byteVariable type variable B */
double pi = 3.14159;        /* declares and assigns a value of PI */
char a = 'a';    

Types of variables :

There are different types of Variables in Java,

1) Local Variables or Method Variables

2) Instance Variables

3) Class or Static Variables

Local Variables

Local variables are the variables which are visible only inside the method in which it is declared. It will not be visible outside the method but you can pass the local variables to functions as a parameter. That means while calling a function the variables which you pass are local variables for that method. And the function who holds those variables are Instance Varibales.

Note :

1) Local variables have no default values so you have to initialise some value when you declare local variables.

2) Access modifiers (public, private ,protected) cannot be used with local variables. Error will be thrown if you used any access modifier with local variables.

3) Local variables are implemented at stack level .

Example :

public class Demo {
   public void sum(){
 /* Local Variables */
        int a = 1;
        int b = 3;
        int sum = a +b;
        System.out.println("Addition is : " + sum);
     }
   
   public static void main(String args[]) {
        Demo demo = new Demo();
        demo.sum();
     }
}

Output :

Addition is : 4

Instance Variables :

1) Instance variables are declared in a class, but outside a method, constructor or any block.

2) Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

3) Access modifiers can be given for instance variables.

4) Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can be assigned during the declaration or within the constructor.

5) When space is allocated for an object in the heap, a slot for each instance variable value is created.

Example :-

public class Demo {

/* Instance Variables */  
    public int a = 1;
    private int b = 3;

   public void sum() {
        
        int sum = a +b;
        System.out.println("Addition is : " + sum);
     }
   
   public static void main(String args[]) {
        Demo demo = new Demo();
        demo.sum();
     }
}

Output :

Addition is : 4

Class or Static Variables

1) Java static variables also called as Class Variables are declared with the static keyword in a class, but outside a method, constructor or a block.

2) There would only be one copy of each java static variable per class, regardless of how many objects are created from it.

3) Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor.

public class Demo {

/* Class Variable */
   private static int a;
     
   public static void main(String args[]) {
      a = 5;
      System.out.println("Value of a is " +a);
     }
}

Output :

Value of a is 5

 

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com