Types of Variables in Java

Types of Variables:

  • Based the type of value represented by the variable all variables are divided into 2

types.

types of variable logo


  1. Primitive variable
  2. Reference variables

Primitive  variables: Primitive variables can be used to represent primitive values.

Example: int x=10;

Reference  variables: Reference variables can be used to refer objects.

Example: 

Student obj=new Student();


  • Based on the purpose and position of declaration all variables are divided into the following 3 types.

  1. Instance variables
  2. Static variables
  3. Local variables

Instance variables

  • If the value of a variable is varied from object to object such type of variables are called instance variables.
  • For every object a separate copy of instance variables will be created.
  • Instance variables will be created at the time of object creation and destroyed at the time of object destruction hence the scope of instance variables is exactly same as scope of objects.
  • Instance variables will be stored on the heap as the part of object.
  • Instance variables should be declared with in the class directly but outside of any method or block or constructor.
  • Instance variables can be accessed directly from Instance area. But cannot be accessed directly from static area.
  • But by using object reference we can access instance variables from static area.

Example:

Instance variable Example

  • For the instance variables it is not required to perform initialization JVM will always provide default values.

Example:

public class HelloWords {

boolean flag;

 

public static void main(String[] args) {

HelloWords obj = new HelloWords();

System.out.println(obj.flag);// false

}

} 

Output:

false


  • Instance variables also known as object level variables or attributes.
             Read more: Reserved keywords in java

Static variables:

  • If the value of a variable is not varied from object to object such type of variables is not recommended to declare as instance variables. We have to declare such type of variables at class level by using static modifier.
  • In the case of instance variables for every object a separate copy will be created but in the case of static variables for entire class only one copy will be created and shared by every object of that class.
  • Static variables will be created at the time of class loading and destroyed at the time of class unloading hence the scope of the static variable is exactly same as the scope of the.class file.
  • Static variables will be stored in method area. Static variables should be declared with in the class directly but outside of any method or block or constructor.
  • Static variables can be accessed from both instance and static areas directly.
  • We can access static variables either by class name or by object reference but usage of class name is recommended.

Example:

public class StaticVaribleDemo {

static int i = 10;

 

public static void main(String[] args) {

StaticVaribleDemo obj = new StaticVaribleDemo();

System.out.println(obj.i);// 10

System.out.println(StaticVaribleDemo.i);// 10

System.out.println(i);// 10

 

}

}

Output:

10

10

10

  • For the static variables it is not required to perform initialization explicitly, JVM will always provide default values.

Example:1

public class Test {

static String s;

public static void main(String[] args) {

System.out.println(s);// null

}

}


Example:2

public class Test {

int x = 10;

static int y = 20;

 public static void main(String[] args) {

Test t1 = new Test();

t1.x = 888;

t1.y = 999;

Test t2 = new Test();

System.out.println(t2.x + "----" + t2.y);// 10 999

}

}

  • Static variables also known as class level variables or fields.

Local variables:

  • Some time to meet temporary requirements of the programmer we can declare variables inside a method or block or constructors such type of variables are called local variables or automatic variables or temporary variables or stack variables.
  • The local variables will be created as part of the block execution in which it is declared and destroyed once that block execution completes. Hence the scope of the local variables is exactly same as scope of the block in which we declared.
Example:1
local variable Error


Example:2
local variable Error

  • The local variables will be stored on the stack.
  • For the local variables JVM won't provide any default values compulsory we should perform initialization explicitly before using that variable.
local variables in java


Note: The only applicable modifier for local variables is final. If we are using any other modifier we will get compile time error.

  • It is never recommended to perform initialization for the local variables inside logical blocks because there is no guarantee of executing that block always at runtime.
  • It is highly recommended to perform initialization for the local variables at the time of declaration at least with default values.
    illegal local variable declaration

Conclusions: 

  • For the static and instance variables it is not required to perform initialization explicitly JVM will provide default values. But for the local variables JVM won't provide any default values compulsory we should perform initialization explicitly before using that variable.
  • For every object a separate copy of instance variable will be created whereas for entire class a single copy of static variable  will be created. For every Thread a separate copy of local variable will be created.
  • Instance and static variables can be accessed by multiple Threads simultaneously and hence these are not Thread safe but local variables can be accessed by only one Thread at a time and hence local variables are Thread safe.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.