Skip to content

Variables

Types of Variables

A. Based on the type of volume 1. Primitive Variables

    int x = 10;
  1. Reference Variables
    Student s = new Student ();
    

B. Based on the position of declaration or behaviour 1. Instance Variable

    class Student{
        String name;
        int rollNo;
    }

    s1.name = "Nishal"  // Student One
    s1.rollNo = 20;

    s2.name = "Biswas"  // Student One
    s2.rollNo = 8;
  • For every object separate copy of name and rollno is created. This type of variable is instance variable.
  • Instance variable are declared within a class and outside of method / block / constructor.
  • Instance variable are created at the time of object creation and destroyed when object is destroyed.
  • They are also known as attributes.

  • Static Variable

  • If the value of variable doesn't varies from object to object , we create static variable

    class Student{
        static String name;
    }
    
    name = "Nishal" // Student One; Student Two
    
    • Are declared inside class but outside of any method / block / constructor.

    • Scope
      ⏩ Created at the time of class loading and destroyed at the time of class unloading.

  • Local Variable

    [method]
    void mOne{
        int x = 10; // is not available outside mOne method.
    }
    
    [block]
    for(int i = 0; i<5; i++){
        System.out.println(i); // is not available outside for loop.
    }
    
    [constructor]
    Test(){
        int x = 10; // is not available outside constructor.
    }
    
  • aka Local / Temporary / Stack / Automatic

  • are stored in stack memory.
  • Scope : ⏩ Destroyed after finishing method / block / constructor
  • For every thread separate, local variable is created.
  • For local variable, JVM won't provide default value. So, we must provide value explicitly before using it.
  • For local variable, the only applicable modifier is final.

Examples

Life Time Of Variables

class LifeTime{
    public static void main(String args[]){
        int x;

        for(x=0;x<3;x++){
            int y=100;  /* Y is initialized every time the loop runs. */
            System.out.println("Value of Y is :"+y);
            y=-1;
            {
                /* I created a new scope here to show a compile-time error. */
                int y;
            }
            System.out.println("Value of Y now is :"+y);
        }
    }
}

Scope of Variables - I

class Scope{
    public static void main(String args[]){
        int x;
        x=10;
        if(x==10){
            int y = 20;
            System.out.println("The value of x : "+x+" and y : "+y);
            x=y*2;

        }
        // y=30; // Y is not known here.

        /* X is still here. */
        System.out.println("Value of x: "+x);
    }
}

Scope of Variables - II

class Scope{
    public static void main(String[] args){
        if(true){
            int i ; // For local variable, JVM won't provide default value sp we must provide value explicitly before using it.
            System.out.println(i); // error: variable i might not have been initialized
        }else{
            // System.out.println(i); //error: cannot find symbol
            System.out.println("Hello Eveyone");
        }
    }
}

Dynamic Initialization of Variables

class DynamicInitialization{
    public static void main(Static args[]){
        double a = 3.0,b=4.0;

        // c is dynamically initialized. It means that it is initialized at the run time.

        double c = Math.sqrt(a*a+b*b);
        System.out.println("Hypotenuse is : "+c);

    }    
}

Type Conversion

class TypeConversion{
    public static void main(String args[]){
        int x;
        short z=30;
        double y=20.5;
        /* This is how implicit type conversion takes place. */
        x=z;
        System.out.println("Value of float Z : "+z+" is converted to int X : "+x);

        /* This is how explicit type conversion takes place. */
        x=(short) y;
        System.out.println("Value of float Y : "+y+" is converted to int X : "+x);

    }
}