Variable Types in Java


Variable Types in Java

We have previously seen about data types in java and some basics of variables. This post is all about Variable Types in Java.

Variable is named memory location to store value of particular data type. Data type here is a java primitive or reference data type.

Data type is essential to represent type, nature and set of operations for the value stored.

Variable Types in Java:

There are three Variable Types in Java:

  1. Local variables
  2. static variables
  3. instance variables

Generally we can divide variables in two types Local and member variables. Member variables are two types static and instance. So static and instance or non static variables may seems a bit similar but they are quite different.

Variable types in Java
Variable types in Java

Local variable

Local variables are declared used and die inside a method or block. These variables come in existence with the execution of the method and are finished when method execution completes.

So there life is in method stack. Generally use of local variables is inside methods but they also exist in blocks.

Scope of local variable is method or block in which it is defined. We must have to initialize after declaration because these are not initialized automatically.

If you try to use local variable with out initializing, you will get compile time error.

Static Variables

Static variables are the variables which are defined inside a class and out side any method and block with static keyword.

These Static variables are also called class level variables therefor name of class is used to access static variables. Generally we use Static variables to represent values that are same for any object of that class.

There is only one copy of static variable that is for class. JVM automatically initialize static variables with default value, When you do not initialize them.

Static variables are directly accessible inside static methods and static blocks. Elsewhere these are accessed with class name.

Static variables get storage in static memory and are end  when program terminates.

Instance variable

variables defined inside class and outside any other method are called instance variables. The copy of instance variable is available to  each object of that class.

These variables are also automatically initialized with the default values according to their data types. These variables are created as part of individual objects inside heap memory.

Instance variable come into existence with the creation of objects and destroyed with object. Instance variables can be accessed using corresponding object name.

These variables can be used without object name or with this pointer  inside the same class.

Variable Types in Java illustration by code..

Class1.java:

package packk;
public class Class1 {
static int i1;
int i2;
void say(){
int x; // x is local variable and its scope is with in say()
// if you try to print x, compile time error
System.out.println("Say method "+i2);
}
// to illustrate static variables are directly used in static blocks
// to illustrate static variables are initialized automatically
static{
System.out.println("static block "+i1);
}
public static void main(String[] args) {
Class1 c1,c2;
c1=new Class1();
c2=new Class1();
c2.i2=21;
// instance variable with object
// default value will be printed
System.out.println("instance c1 "+c1.i2);
//different copy of i1 for another object
System.out.println("instance c2 "+c2.i2);
}
}

Output of this code will be like this.

static block 0
instance c1 0
instance c2 21

Hope you got clear idea about variables in java but If you have any further query, please comment down.

Have any Question or Comment?

Leave a Reply

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