Different types of Variables in Java – Based on it’s Value Representation and it’s Declaration Position

Different types of Variables in Java

Different types of Variables in Java

 

Here Different types of Variables in Java are like Instance, Static, Local, Non-static, Primitive, Reference variables,etc. These all are different types of variables available. we can classified them based on type of value representation and based on it’s declaration position, So let’s see them in details…

Based on types of Value Representation :-

Based on on types of value representation by a variable we can divide them into two different types of variables in java:

  1. Primitive Variable

  2. Reference Variable

 

Primitive Variable :

A variable which contains directly a value. ex. byte, short, char, int, long, float, double, boolean.

int x = 10; // where x is a primitive variable

Reference Variable :

A variable which refers to any object.

Test t = new Test(); // where t is a reference variable

 

Also Read: Different Java Data Type – Primitive and Non-Primitive Data type

Also Read: 14 Difference between C language and Java language

Also Read: Basic differences between C++ and Java Programming language

 

Based on Position of Variable Declaration :-

Based on position of variable declaration we can divide them into three different types of variables in java:

  1. Instance Variable
  2. Static Variable
  3. Local Variable

 

Instance Variable:

A variable which value is vary from object to object and it will generate a separate copy for every Object of that class.

Instance Variable is declared within a class but it should be outside from methods, constructors, blocks, etc.

Scope of Instance Variable is exactly same as scope of object of that class.

We can not access instance variable directly from static area with the help of object reference we can access them. But in instance area we can directly access Instance variable.

Instance variable is also know as Object level variable or attribute or Non-static variable.

JVM provides default value for instance variables like

Data Type  Default Value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
boolean false
char ‘u0000’
String(or any Object) null

 

class Test
{
   int x = 10; // instance variable

   public static void main(String[] args)
   {
      Test t = new Test();
      System.out.println(t.x);  // we can not access directly here, because main method is static
   }
}

 

Static Variable:

Static variable will generate a single copy of class level and shared by every objects of that class.

It is created when class is load to JVM and destroyed when class is unload from JVM.

Scope of the static variable is same as scope of that .class file.

We can access static variable directly from both instance area as well as static area.

For static variable JVM will also provides default value.

Static variable is also known as Class-Level Variable or fields.

 

class Test
{
   static int x = 10; // static variable

   public static void main(String[] args)
   {
      Test t = new Test();
      System.out.println(x);  // we can access directly here in static area.
   }

   void disp()
   {
      System.out.println(x);  // we can access directly here in non-static area. 
   }
}

 

Local Variable:

A variables which are declared inside methods,blocks, constructor or in control structure(loop or condition) all are known as local variable.

It is declared for temporary requirement by programmer in program.

Scope of that variable is within that block, method or constructor.

JVM will not provides default values for local variable.

Local variable is also known as Temporary variable or stack variable.

 

class Test
{
   public static void main(String[] args)
   {
      for(int i=0; i<=5; i++)   // here i is a local variable
      {
         System.out.println(i);
      }      
   }
}

So I hope this blog gives a clear concept about Different types of Variables in Java.

Thank you and Stay Connected with Place4Java and follow on Facebook Page.

Be the first to comment

Leave a Reply

Your email address will not be published.


*