Java Variable Types
Variables are name of reserved memory location inside Program.Based on Position of Declaration and behavior all Variables are divided into three categories.
- Local Variable
- Instance Variable
- Static Variable
Local Variable : Variable that is declared inside method is called Local Variable.
Instance Variable : If a variable value varied from object to object then such type of variable are called Instance Variable.
In case of Instance Variable a separate copy of instance variable is created.Where we have to declare Instance Variable ?
Instance Variable should be declared within class directly, but outside of any method or block or constructor.
Note: As we discussed previously variable declared inside any method or block or constructor as known as Local Variable.
Note: As we discussed previously variable declared inside any method or block or constructor as known as Local Variable.
class InstanceVariable{
int x=10;
public static void main(String a[]){
}
}
When Instance Variable will be created ?
Instance Variable is created at time of Object creation.because Instance Variable is part of object.
What is scope of Instance Variable ?
Instance variable is created at time of object creation hence it will be destroyed at time object destruction.
Where Instance Variable is stored?
As part of Object Instance variable will be stored inside heap memory.
Some Important points regarding Instance Variable ...
We can't access Instance Variable directly from static area it means we can't access instance variable inside main method.
Instance Variable is created at time of Object creation.because Instance Variable is part of object.
What is scope of Instance Variable ?
Instance variable is created at time of object creation hence it will be destroyed at time object destruction.
Where Instance Variable is stored?
As part of Object Instance variable will be stored inside heap memory.
Some Important points regarding Instance Variable ...
We can't access Instance Variable directly from static area it means we can't access instance variable inside main method.
public class InstanceVariable {
int x=10; //Instance Variable
public static void main(String a[]){
System.out.println(x);
}
}
After that we obtain error as shown below
error: non-static variable x cannot be referenced from a static context
System.out.println(x);
To solve such type of problem we access Instance variable as Reference Variable inside main method.
public class InstanceVariable {
int x=10; //Instance Variable
public static void main(String a[]){
InstanceVariable iv=new InstanceVariable();//creating an object
System.out.println(iv.x); //accessing Instance Variable as Reference variable
}
}
For Instance Variable JVM always provide default values.Example:
public class InstanceVariable {
int x; //Instance Variable Integer
String name;//Instance Variable String
boolean b;//Instance Variable boolean
double d;//Instance Variable double
public static void main(String a[]){
InstanceVariable iv=new InstanceVariable();//creating an object
//accessing Instance Variable as Reference variable
System.out.println(iv.x);
System.out.println(iv.name);
System.out.println(iv.b);
System.out.println(iv.d);
}
}
Comments
Post a Comment