Variables in java


Variable is nothing but name of memory location in java.Each variable has the specific data type which defines size and layout of variable’s memory.
There are three kinds of variables in java.

Local variable:

A Variable which is declared inside a method can be termed as “Local Variable”. It is mandatory to initialize local variable otherwise Compiler will complain about it.

Instance variable:

A Variable which is declared at class level can be termed as “Instance variable”. It is not mandatory to initializeInstance variable.

All instance variable will be by default initialized by JVM.

Static variable:

A Variable which is declared as static is known as “Static variable”. Static variables are class level variables.

Let’s understand it with the help of simple program:

When you run above program, you will get below output:

Instance variable: 0
Static variable: 20
Method local variable: 10

That’s all about Variables in java.


Add Comment