Variable in Java


Variable in Java

Variable is a name of memory location. Variables are used to hold the value. Example

int data=50; //Here data is variable

Types of Variable

There are three types of variables.

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

1.Local variable:

A variable that is declared inside the method is called local variable

2.Instance variable:

A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.

3. Static variable:

A variable that is declared as static is called static variable. It cannot be local

Example to understand the types of variables

class abc

{

int a=20;   //Instance variable

static int b=100;   //Static variable

 

void method1()

{

int c=50;   //Local variable

System.out.println(“Local variable c=”+c); // print Local variable

}

public static void main(String aa[])

{

abc a1=new abc();

System.out.println(“Instance variable a=”+a1.a); // print Instance variable

System.out.println(“Static variable b=”+a1.b); // print Static variable

a1.method1(); // print local variable

}

} //end of class

Comments are closed.