Instance variables

Instance variables belong to an object and each object has its own copy of instance variables.
Thus, they represent the state of an object.
Similar to instance(or class) variables, there may also be methods that are defined inside a class.
They are called class methods or instance methods.

Example of instance variables

Below is a class Car which contains two instance variables color and model and a class method printCar which prints the values of both these variables.

public class Car {
   /*
    * Instance variables
    */
   String color;

   String model;

   /*
    * Constructor
    */
   public Car(String color, String model) {
      this.color = color;
      this.model = model;
   }

   /*
    * Instance method
    */
   public String printCar() {
      System.out.println("Car color: " + color);
      System.out.println("Car model: " + model);
   }
}

Let us create a couple of objects of this class

Car objOne = new Car(“Red”, “i10);

Car objOne = new Car(“Blue”, “i20);

This declaration creates two objects with different values of color and model variables.
Now, when printCar method is called on each of this object, look at the output.

Car color: Red
Car model: i10
Car color: Blue
Car model: i20

Above output shows that each object has a different value for the instance variables.
As mentioned earlier, instance variables represent the state of an object.
Thus, one object represents a red car with a different model and another object represents a blue car with another model.

Accessing Instance variables

Instance variables can only be accessed using an object of the class in which they are declared.
Instance variables are accessed by their name using the dot operator and the object.
Example,

objOne.color

where objOne is the name(or reference) of an object and color is the name of instance variable.

If you are accessing an instance variable inside the same class, then there is no need to use object name and dot before the variable
It can be accessed simply by its name or using this.color where this refers to the current object.

instance variables in memory java

Instance variables in memory

Each object has its own copy of instance variables. Above two objects will be represented in memory as shown below.

Default values of instance variables

It is also possible to define a default value of instance variable at the time of declaring the variable.
If an instance variable is given a default value, then each object will have the same value for that variable automatically initialized so that you do not want to set its value explicitly.
But you can always override the default value for a particular object.
Example,

public class Book {

   String title;

   String type = "paperback";

   // other fields and methods
}

Above class has two instance variables. One out of these is given a default value.
Every object  created will have the same value for that variable unless overridden.
If instance variables are not given any value, then they are automatically initialized by the JVM as per their data type as below.

Data typeDefault value
byte0
short0
int0
long0
float0.0
double0.0
char‘\u0000′(null character)
booleanfalse
Stringnull
Objectsnull
Arraysnull

Scope of instance variables

Instance variables support all the four access modifiers(or specifiers) in java and can thus, be marked public, private, protected and default(without any specifier).
This means that their scope(or visibility) can be limited to a class, package or they can be made visible everywhere.

If they are marked private, then getter and setter methods should be created to retrieve and modify their values. These getter and setter methods should always be public.

Remember that instance variables can only be accessed using an object of their class and there is no other way to access these.
Besides access modifiers, following are the other keywords that may be applied to instance variables.

  • Instance variables can not be abstract.
  • They can not by synchronized.
  • They can be final.
  • They can be transient.
  • They can be volatile.
  • They can be marked static with static keyword. If they are marked static, then they are no longer called instance variables.
  • They can not be strictfp.
  • They can not be native.
  • They can be public, private, protected or without any specifier.

Hope this post was useful for you.

Leave a Reply