Instance variables are those variables which are declared inside a class. These instance variables are associated with the instances of class rather than to class. In other words every instance of class will its own copy of instance variables.
In following example we will see how to define, initialize and use instance variables under different scenarios.
Now the important thing to be remembered by beginners is that there is difference between defining/declaring a variable and initializing the variable. Declaring or defining the variable means just type name of the data type followed by variable. Initializing a variable means defining the variable and giving/assigning some value to it. But remember you can assign only compatible values to the variables. For ex. if you are declaring a variable of type then you can assign only int type values to it. And this paragraph is completely true for all variable types in Java i.e.:Instace, Static and local variables.
The one more important thing about instance variable is that if you do not initialize your instance variable then the compiler will initialize the instance variable with the defualt value of type with which it is declared. So if you define instance variable of type int and if you do not initialize it then compiler will initialize that instance variable with default value for int which is '0'(Zero). The default value for String is null; for float is 0.0; for boolean false; for char it is the unicode number zero. The instance variables are always declared inside the class. You can not declare the instance variable inside method, block of code, constructor etc.
The instance variables have life span of object i.e. instance variables are given space in memory only when you create an instance of object using the keyword 'new'. When object is destroyed the variables from the memory are also destroyed. As the instace variables are completely attached to the object they are stored in heap area along the object to which it belongs during their life time.
class InstanceVariable{
int a=20;
//Declaring and initializing the variable.
private int c=40;
int b;
//Declaring the variable.
public InstanceVariable(){
//Instance variables can be accessed directly by other class members.
System.out.println("Accessing the instance variables within the same class using constructor of same class:\n");
System.out.println(a);
System.out.println(c);
/*Private instance variables can also be directly accessed by members of same class*/
print();
//Calling method of class from constructor of same class.
}
public void print(){
System.out.println("Accessing the instance variables within the same class using method in same class:\n");
System.out.println(a);
/*Here method print() is member of class and so it can access the instance variable directly*/
System.out.println(c);
}
public static void main(String[] args){
/*System.out.println(a);
// So the important point to learn here is that you can not access the non static variable(or anything is static) from static context. Remember the main() method is static here as always it will in Java*/
InstanceVariable iv=new InstanceVariable();
Ping p=new Ping();
Pong o=new Pong();
}
}
The following class inherits the class InstanceVariable
class Ping extends InstanceVariable{
public Ping(){
//Constructor for class Ping.
System.out.println("Accessing the instance variables from child class:\n");
System.out.println(a);
/*Non-private members of super class can be accessed directly in child class because inheritance means compiler treats non-priavte *super class variables as declared by child class itself.*/
/*System.out.println(c);
Here we can see that you can not access the private members of super class in child class.*/
}
}
The next section code tries to access the instance variables declared in another and these classes do not have any inheritance tree.
class Pong{
public Pong(){
System.out.println("Accessing the instance variables declared in another class:\n");
/*System.out.println(a);
Here you can notice that we cannot access the instance variables declared in another directly (i.e. without using the name of instance variable of class) */
InstanceVariable i=new InstanceVariable();
/*The instance variable in another class can be accessed using the instance of the class where the instance variable is declared. */
System.out.println(i.a);
/*System.out.println(i.c);
But the private members of the class cannot be accessed outside the class where they are declared already; in any way.*/
}
}
0 comments:
Post a Comment