Thursday, 30 March 2017

class variable vs instance variable in java

Before we proceed to find out the difference between class variable and instance variable, we must be aware that both of them are member variables. Both of these variables are associated with the class.

class variable vs instance variable Java radar
Member Variables


Instance variable:

int seasonName;
in above diagram is an instance variable.

Meaning is that every time an instance of the class Season is created, each instance will have its own copy of seasonName variable. If any of these instance modify the value of this variable then it will impact only that particular instance not the others.

So we can say that instance variable can be assigned with different value by each of different instances of the class.

As every instance has its own copy of the variable so memory requirement increases for the maintenance of instance variable for every instance.

In example program shown below, you can see each Human instance male, female, male1, etc can assign their own value to name variable. And if anyone of them later try to change the value it will only impact that particular instance without effecting other instances at all.


Example:

Source FileInstanceVariable.java

package variables;

public class InstanceVariable {

      public static void main(String[] args) {
            //assign different value to name(instance variable)of each instance
            Human male = new Human();
            male.name = "Rajneesh";

            Human female = new Human();
            female.name = "Shweta";

            //instance male assign value to name instance variable: Rajneesh
            System.out.println("Name of male:"+ " " + male); 
            //instance female assign value to name instance variable: Shweta
            System.out.println("Name of female:"+ " " + female); 

      }

}


Source FileHuman.java


package variables;

public class Human {
   public String name//instance variable  
  
   //overriding toString()
   public String toString(){
            return name;
      }

}


OUTPUT: 
Name of male: Rajneesh
Name of female: Shweta


Note:

For once just remove, 
public String toString(){return name;}
and see what you get?

You will get output something like this:
Name of male: variables.Human@15db9742
Name of female: variables.Human@6d06d69c




Class variable:

NOTE: static variable belongs to the class rather than to the instance of the class. It is variable that is common to all the objects. And this is achieved by declaring variable static.

static int seasonCount;
in above diagram is a class variable or static member variable.

Meaning all the instances of the class Seasons will share the same copy of the variable seasonCount.

And if any instance changes the value of the class variable then all the instances sharing this variable's data will get impacted.

As same copy is shared by every instance so memory consumption is less in comparison to maintenance of instance variable.

Try to write an example for static variable by yourself taking reference of instance variable example shown above. For reference you can follow the oracle documentation for class variable

Note: add static keyword to instance variable to change it to class variable.

int count;. // Instance variable
static int count; // class variable



You may also like to read:
                                                        

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.