Variable in JAVA | Types of Variables - FindersAdda : Tutorials of Make Money, Computer or Internet

Monday, 20 November 2017

Variable in JAVA | Types of Variables


A variable is a data name which is used to store data value. Java variables are typically used to store information which your java program needs to do its job.

Types Of Variables

There are two types of variable which is based on types of value represented by a variable.
  1. Primitive Variable
  2. Reference Variable

Primitive Variable : - Primitive Variables are used to represent primitive values. For example int x = 500;

Reference Variable : - Reference Variable are used to refer objects.

Student stu = new Student();
Here, stu is the object name which is also called variable name that refers to object.


There are three types of variable based on Scope that means purpose and position.
  1. Instance Variable
  2. Static Variable
  3. Local Variable


Instance Variable : - 


A variable which is declared within the class directly but outside of any method, block and constructor then this type of variable is called instance variable.

If the value of a variable is varied from object to object such type of variables are called instance variable.

JVM provide default value for instance variable according to its data type. For int data type JVM provide default value for instance variable is 0 and so on.

For every object a separate copy of instance variable will be created.

Instance variable is created at the time of object creation and destroyed at the time of object destruction. Hence, the scope of instance variable is exactly same as the scope of object.

Instance variable is also known as object level variables or attributes.

Instance variable is stored in the heap memory as the part of object. We can't access instance variables directly in static area but we can access by using object reference. We can access instance variables directly from instance area.



Output of the above program


Static Variable : - 


A variable which is declared with static keyword within the class directly but outside of any method or constructor then this type of variable is called Static variable.

The value of static variable is not varied from object to object.

Static variable is also known as class level variables or fields.


JVM provide default value for static variable according to its data type. For int data type JVM provide default value for static variable is 0 and so on.


A single copy is created at the class level and shared by every object of the class. Static variable are initialized only once.

Static variable is created at the time of class loading and destroyed at the time of class unloading. Hence, scope of static variable is exactly same as scope of .class file.

Static variable is stored in method area. We can access static variable either by object reference or by class name but recommend to use class name. Within the same class, it is not required to use class name, we can access it directly.

Static variable is directly accessed within the static member and non-static member function.


Output of the above program

Local Variable : - 


A variable which is declared within a method, block or constructor then this type of variable is called local variable. Local variable is also known as temporary variable, stack variable and automatic variable.

Local variable is stored inside stack memory. Local variable is created while executing the block in which we declared it. Once block execution completed automatically local variable is destroyed. Hence, the scope of local variable is the block area that means which block it is declared.

The only applicable modifier for local variables is final.

JVM doesn't provide default value for local variable, we have to perform initialization explicitly before using that variable. If we are not using local variable then it is not required to perform initialization.


Output of the above program




You May Also Read: - 




About Author

SUMIT KUMAR GUPTA
SUMIT KUMAR GUPTA

Author & Editor

Myself Sumit Kumar Gupta & 21 year's old person. I am a programmer and blogger. I spend much more time on programming and blogging and helps other programmers and bloggers.

0 comments:

Post a Comment

We are eager to see your comments.
Comment Rule :

1. Don't use any link in the comment box. If you have any problem related to link then contact me with the contact form.

2. Don't use the wrong word if you write the wrong word in comment box then it will get spam.