🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Variables in Java  


Introduction

  • A variable is the name of memory location that can store data.
  • In simple words we can say, variables are the containers used to store the data values.

  • Real World Example :

    Variables in Java Real World Example

  • Java Example :
    int rollno = 101;
    Variables in Java
    Here data i.e. 101 is stored in memory, variable name i.e. rollno points to that memory and variable has its data type i.e. int.
  • So we can say that every variable has its :-
    • Data Type : The type of data that it stores; for example int, char, long, float etc.
    • Variable Name : The unique name within the scope which points to the memory location.
    • Value : The data assigned to the variable.

    • Variables in Java in Java

  • More Points about Varibales in Java :
    • Java is a statically typed language, so we must declare the type of data a variable will store.
      For example if we create variable rollno having data as 101, then we have to specify its data-type i.e. int.

    • The value stored in a variable can change during program execution, which is why it is called a "variable“.
      For example we have one variable no having 10 value, we can change its data as below :
      int no = 10;
      System.out.println("no : "+no);     //output is no : 10
      
      no = no + 20;
      System.out.println("no : "+no);     //output is no : 30

Types of Data Types

There are 3 types of variables in Java which are as below :-
   1. Local Variable
   2. Instance Variable
   3. Static Variable

1. Local Variables
  • A variable defined within a block, method, or constructor is called a local variable.
  • Local variables are created when the block or method is executed and destroyed when the block or method exits.
  • The scope of a local variable is limited to the block in which it is declared; it cannot be accessed outside that block.
  • We have to initialize the local variables before using it.
2. Instance Variables
  • A variable defined inside a class but outside any method, block, or constructor is called an instance variable.
  • Instance variables are created when an object of the class is instantiated and destroyed when the object is garbage collected.
  • Each object of the class has its own copy of instance variables, so their values can vary between objects.
  • If we do not explicitly initialize the instance variables, then default values are assigned to them based on their data types (e.g., 0 for integers, null for objects).
3. Static Variables
  • A variable defined with the static keyword inside a class is called a static variable.
  • Static variables are shared among all objects of the class; a single copy is created and stored in the memory.
  • These variables are created when the class is loaded and destroyed when the class is unloaded.
  • Static variables can be accessed without creating an object, using the class name.
Program for Local, Instance & Static Variables
public class MainApp
{
    int no = 100;   //instance variable

    static int sno = 200;   //static variable

    void m1()
    {
        int no1 = 10;   //local variable
        System.out.println("Result 1 : "+(no1 + no));
        System.out.println("Result 2 : "+(no1 + no + sno));
        //System.out.println("Result 3 : "+(no1 + no2));    //error as no2 is local variable which is present in m2() method and thus cannot be used outside m2() method
    }

    void m2()
    {
        int no2 = 20;   //local variable
        System.out.println("Result 4 : "+(no2 + no));
        System.out.println("Result 5 : "+(no2 + no + sno));
        //System.out.println("Result 6 : "+(no2 + no1));    //error as no1 is local variable which is present in m1() method and thus cannot be used outside m1() method
    }

    static void m3()
    {
        int no3 = 30;   //local variable
        //System.out.println("Result 7 : "+(no3 + no)); //error because no is instance variable which cannot be used inside static method
        System.out.println("Result 8 : "+(no3 + sno));
        System.out.println("Result 9 : "+(no1 + no2 + no3));    //error because no1 and no2 are local variables and cannot be used outside their method scope
    }

    public static void main(String[] args)
    {
        MainApp obj = new MainApp();
        obj.m1();
        obj.m2();

        MainApp.m3();  //static method can be directly called by class name
    }
}
Output:
Result 1 : 110
Result 2 : 310
Result 4 : 120
Result 5 : 320
Result 8 : 230