Variables in Java


☞A named memory location, whose contains can be changed with in program execution is known as variable.

☞When you run any program, it consumes memory in RAM till its execution in process.

Examples : int counter; String name; int[] ages; char letters[];

☞String is a predefined class not a primitive data type.

Note : Predefined class name always have first letter in capital.

Types of Variables in Java

☞Three types of variables in java : local variables, instance variables, and static variables.

local variables

☞A variable which is declared inside the method is called local variable.

instance variables

☞A variable which is declared inside the class but outside the method is called instance variable.

☞It is not declared as static.

static variables

☞A variable that is declared as static is called static variable.

☞It cannot be local.

Example

class TypesOfVariables{	
    int data=50;      //instance variable                               
    static int m=100; //static variable                           
    void method(){  
       int n=90;      //local variable                                  
    }  
}