Variable

The Variable is a basic unit of storage in a java program.A variable can be defined by the combination of an identifier,a type and an optional initializer.In addition,all Variables have a scope,which defines their visibility,and a lifetime.

There are 3 type of variable-

Local Variable-A Variable that is declared inside method Known as Local Variable.

Instance Variable-A Variable that is declared inside class but outside method is called Instance Variable.It is not declared as static.

Class/Static Variable-A Variable that is declared as static is called static(also class)variable.It cannot be local.

Local Variable

  • Local Variable are declared in methods,constructor or blocks.
  • Local Variable are created when the method,constructor or block is entered and the variable will be destroyed once it exit the method,constructor or block
  • Access modifier cannot be used for Local Variable.
  • Local Variable are visible only within the declared method ,constructor or block.
  • Local Variable are implemented at stack level internally.
  • Here's method that declare a local variable named i,and then initialize variable before using it:
public static void main(String[] args)
{
int i;
i=0;
System.out.println("i is="+i);
}

Example

public class Test{
public void age(){
int age=0;//Initializing with 0
age=age+10;
System.out.ptintln("Age is ="+age);
}
public static void main(String[] args)
{
Test ob=new Test();//Creating Object
ob.age();
}
}

Output

Age is =10

Instance Variable

  • Instance variable are declared in a class,but outside the method,constructor or any block.
  • When a space is allocated for an object in the heap,a slot for each instance variable value is created.
  • Instance variable is created when an object is created with the use of keyword 'new'  and destroyed when an object is destroyed.
  • Instance variable hold values that must be referenced by more than one method,constructor or block
  • Instance variable can be declared in class level before or after use.
  • Access Modifier can be given for Instance Variable.
  • The Instance Variable are visible for all method,constructor or block in the class.Normally,It is recommended to make these variable private(access level)
  • Instance variable can be accessed directly by calling the variable name inside the class.

Example

class Rectangle{
//Instance variable
double length;
double breadth;
//This class declare an object of type Rectangle
public static void main(String[] args)
{
Example rect=new Example();
//Assign Value to rect's Instance Variable
rect.length=10;//Using dot(.) operator we can access variable of the class
rect.breadth=20;
//Compute area of rectangle
area=rect.length*rect.breadth;
System.out.println("Area is ="+area);
}
}

Output

Area is=200

Class/Static Variable

  • Class Variable are also known as Static Variable are declared with the static keyword in the class,but outside method,constructor or block
  • There would only be one copy of each class variable per class,regardless of how many object are created from it.
  • Static Variable are rarely used other than being declared as constant.Constant are variable that are declared as public/private,final and static.Constant Variable never change from their initial value.
  • Static Variable are stored in static memory.It is rare to use static variable other than declared final and used as either public or private constant
  • Static Variable are created when the program starts and destroyed when the program stops.
  • Visibility is similar to instance variable.However,most static variable are declared public since they must be available for the user of the class.
  • Static Variable can be accessed by calling with the class nameClassName.VariableName

Example

public class Employee
{
//salary variable is private static variable
private static double salary;
//DEPARTMENT is the constant
public static final String DEPARTMENT="Development";
public static void main(String args[])
{
salary=1000;
System.out.println(DEPARTMENT +"average salary:"+ salary);
}
}

Output

Development average salary:1000.0