All Types of Variables in Java

All Types of Variables in Java:

In Java we have different names for variable types such as class variable, static variable, member variable, block variable, instance variable & local variable. Sometimes we will get the confusion between these variables.


All Types of Variables in Java:


  • Member Variable
  • Class Variable
  • Static Variable
  • Instance Variable
  • Block Variable
  • Local Variable



Member variables are class variables.


package in.javadomain;

public class MemberVariables {

// instance variable
int instanceMemberVariable;

// class variable/static variable
static int staticMemberVariable;



Any variable declared for class level is called Member variable.


Member variables are of two types:

1. Static Member Variable/Static Variable/class variable

Class level variable declared with static keyword/static class level variables are called as static variable or class variable.


It will be shared between the different class instances/objects.

It is per class variable. Till the class live in the memory static variables are also alive.


2. Instance Member Variable/Instance Variable

Class level variable declard without static keyword/non-static class level variables are called as instance variable.

It is per instance/per object variable and it lives in the memory till the instance or object exist.

Local variable:


local variabls are created inside the functions/methods.


package in.javadomain;

public class LocalVariables {
public static void main(String[] args) {
int localVariable;



Block variable:


Block variabls are created/declared/used inside the loops/blocks. Eg: for loop/if/else etc.


package in.javadomain;

public class BlockVariable {
public static void main(String[] args) {
for(int i=0;i<10;i++){
// i is a blockVariable here.


Why static local variables are not allowed in java [or]

Why Can’t I declare a static variable inside the Main method?

Since static variables are shared between the different objects/instances, No use of allowing static variables inside the method, because it allows to keep the same static value in all the objects/instances whichever calls this method. Which looks not fair and waste. So static variables are allowed only in class level not for method levels.



 1,032 total views,  1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.