JAVA VARIABLES

 

JAVA VARIABLES

Variables is a name of memory location There are three types of variables in java

  1. Local variable 
  2. Instance Variable 
  3. Static Variable

 LOCAL VARIABLE

A local variable which is declared inside the body of the method is called local variable

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

EXAMPLE:

package variables;

public class localvariable {

    public static void main (String arge[]) {

        int a = 10; //local variable

        System.Out.Println(a);   

    }

}

OUTPUT:

10

 INSTANCE VARIABLE

 A Instance variable which is declared inside the class but outside of the body of the method is called instance variable.

It is not declared as static, and the value is instance specific but is not shared among instances

EXAMPLE:

package variables;

public class instacevariable {

        int a = 10; //Instace variable

        public static void main (String arge[]) {

        instacevariable iv = new instacevariable();

        System.Out.Println(iv.a);   

    }

}

OUTPUT:

10


 STATIC VARIABLE

A variable which is declared as static is called static variable it cannot be local. 

EXAMPLE:

package variables;

public class staticvariable {

        Static int a = 10; //static variable

        public static void main (String arge[]) {

        System.Out.Println(iv.a);   

    }

}

OUTPUT:

10