A. Based on the type of volume
-
Primitive Variables
int x = 10; -
Reference Variables
Student s = new Student ();
B. Based on the position of declaration or behaviour
-
Instance Variable
class Student{ String name; int rollNo; } s1.name = "Nishal" // Student One s1.rollNo = 20; s2.name = "Biswas" // Student One s2.rollNo = 8;
- For every object separate copy of name and rollno is created. This type of variable is instance variable.
- Instance variable are declared within a class and outside of method / block / constructor.
- Instance variable are created at the time of object creation and destroyed when object is destroyed.
- They are also known as attributes.
- Static Variable
-
If the value of variable doesn't varies from object to object , we create static variable
class Student{ static String name; } name = "Nishal" // Student One; Student Two -
Are declared inside class but outside of any method / block / constructor.
- Scope
⏩ Created at the time of class loading and destroyed at the time of class unloading.
- Scope
-
Local Variable
[method] void mOne{ int x = 10; // is not available outside mOne method. } [block] for(int i = 0; i<5; i++){ System.out.println(i); // is not available outside for loop. } [constructor] Test(){ int x = 10; // is not available outside constructor. }
- aka Local / Temporary / Stack / Automatic
- are stored in stack memory.
- Scope : ⏩ Destroyed after finishing method / block / constructor
- For every thread separate, local variable is created.
- For local variable, JVM won't provide default value. So, we must provide value explicitly before using it.
- For local variable, the only applicable modifier is final.
{% include 'variables/lifeTImeOfVariable.java' %}{% include 'variables/scopeOfVariables.java' %}{% include 'variables/scopeOfVariablesOne.java' %}{% include 'variables/dynamicInitializationOfVariables.java' %}{% include 'variables/typeconversion.java' %}