Instance Variable Tutorial

An instance variable is a variable declared in a class that has a separate copy (value) for each object created. This tutorial will expand on the Class Introduction tutorial by demonstrating how to create and use instance variables in our Box class. One thing that every Box has in common is dimensions. A Box has length, width, and height. We are going to declare variables that represent each of those values. These variables are defined as instance variables. Then we will create three types of boxes: small, medium, and large - these will represent three distinct objects and each one will have different dimensions. Finally, we will create a method in the Box class that will display the volume of each box object to the console.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md InstanceVariable
C:\Java>cd InstanceVariable
C:\Java\InstanceVariable>Notepad Box.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Box {
    int length = 0; // instance variable - every Box object will have its own "instance" (copy) of this variable
    int height = 0; // instance variable - every Box object will have its own "instance" (copy) of this variable
    int width = 0; // instance variable - every Box object will have its own "instance" (copy) of this variable

    // create a method to display the volume to the console
    void displayVolume() {
        System.out.println(length * height * width);	
    }
}

Now switch back to the command prompt (CMD) and type in javac Box.java and press Enter.
Switch badk to the CMD prompt and type in the following command.

C:\Windows\System32>cd \
C:\Java\InstanceVariable>Notepad InstanceVariable.java


Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class InstanceVariable {
	public static void main(String args[]) {
		Box small; // declare a reference variable to a Box object 
		small = new Box(); // allocate a new Box object to small
		
		Box medium = new Box(); // single statement - declare reference variable and allocate new Box object
		Box large = new Box();
		
		// object.member, small is its own object
		small.length = 5; // small has its own instance (copy) of the length instance variable
		small.width = 5; // small has its own instance (copy) of the width instance variable
		small.height = 5; // small has its own instance (copy) of the height instance variable

		// object.member, medium is its own object
		medium.length = 10; // medium has its own instance (copy) of the length instance variable
		medium.width = 10; // medium has its own instance (copy) of the width instance variable
		medium.height = 10; // medium has its own instance (copy) of the height instance variable

		// object.member, large is its own object
		large.length = 20; // large has its own instance (copy) of the length instance variable
		large.width = 20; // large has its own instance (copy) of the width instance variable
		large.height = 20; // large has its own instance (copy) of the height instance variable

		System.out.print("The volume of our small box is: ");
		small.displayVolume();
		
		System.out.print("The volume of our medium box is: ");
		medium.displayVolume();

		System.out.print("The volume of our large box is: ");
		large.displayVolume();
	}
}

Now switch back to the command prompt (CMD) and type in javac InstanceVariable.java and press Enter.
Now type in java InstanceVariable and press Enter.


C:\Java\InstanceVariable>javac InstanceVariable.java
C:\Java\InstanceVariable>java InstanceVariable
The volume of our small box is: 125
The volume of our medium box is: 1000
The volume of our large box is: 8000


Final thoughts

I would like to reiterate a few things at this time. In the Class Introduction tutorial I stated that "a class is a template or blueprint from which objects are created." The Box class begins to give you a small example of that definition. The Box class defines the following instance variables: length, width, and height. It also has a method for calculating volume. So as you can see the "template" or "blueprint" is the same for each of the three Box objects that we created: small, medium, and large.


Tutorials