Instance Variables
- If value of variable is changing from one object to another object that type of variable is known as instance variable.
- We can define instance variable are inside the class and outside of any method/block/constructor.
- If we didn't provide the values for instance variable then JVM will provide default values to instance variables.
Note
- The possibility of declaration of instance variables.
- final
- abstract
- static
- strictfp
- synchronized
- native.
--------------------------------------------------------------------------
Program : Sample program on instance variable
Program name: VarDemo1.java
Output :
Id is : 1
Name is : Subbu
Id is : 2
Name is : Basha
class Master
{
int id;
String name;
}
class VarDemo1
{
public static void main(String args[])
{
Master m1 = new Master();
m1.id = 1;
m1.name = “Subbu”;
Master m2 = new Master();
m2.id = 2;
m2.name = “Basha”;
System.out.println("Id is : "+m1.id);
System.out.println("Name is : "+m1.name);
System.out.println();
System.out.println("Id is : "+m2.id);
System.out.println("Name is : "+m2.name);
}
}
Compile : javac VarDemo1.java
Run : java VarDemo1
Output :
Id is : 1
Name is : Subbu
Id is : 2
Name is : Basha
--------------------------------------------------------------------------
Thanks for your time.
Thanks for your time.
Nireekshan