Java Reference
In-Depth Information
name
gender
An instance of Human class
in memory
Figure 6-1.
An instance of the Human class in memory created by the new Human() instance creation expression
The figure shows that memory is allocated for instance variables
name
and
gender
. You can create as many
instances of the
Human
class as you want. Each time you create an instance of the
Human
class, Java runtime allocates
memory for
name
and
gender
instance variables. How much memory is allocated for an instance of the
Human
class?
The simple answer is that you do not know exactly how much memory is used by an instance of a class. In fact, you
do not need to know how much memory is needed to create an instance of a class. The Java runtime takes care of
memory allocation as well as deallocation automatically for you.
Now, you want to move a step forward and want to assign values to
name
and
gender
instance variables for the
newly created instance of the
Human
class. Can you assign values to
name
and
gender
instance variables of the newly
created instance of the
Human
class? The answer is no. You cannot access
name
and
gender
instance variables, even
though they exist in memory. To access instance variables of an instance of a class, you must have its reference
(or handle). The expression
new Human()
creates a new instance of the
Human
class in memory. The newly created
instance is like a balloon filled with Helium gas left in the air. When you release a Helium-filled balloon in the air, you
lose control of the balloon. If you attach a string to the balloon before releasing it in the air, you can control the balloon
using the string. Similarly, if you want to have control (or access) to an instance of a class, you must store the reference
of that instance in a reference variable. You control a balloon with a string; you control a television with a remote
controller. The type of controlling device depends on the type of the object that you want to control. Similarly, you need
to use different types of reference variables to refer to (or to handle, or to work with) instances of different classes.
The name of a class defines a new reference type in Java. A variable of a specific reference type can store the
reference of an instance of the same reference type in memory. Suppose you want to declare a reference variable,
which will store a reference of an instance of the
Human
class. You will declare the variable as shown:
Human jack;
Here,
Human
is the class name, which is also a reference type, and
jack
is a variable of that type. In other words,
jack
is a reference variable of
Human
type. The
jack
variable can be used to store a reference of an instance of the
Human
class.
The
new
operator allocates the memory for a new instance of a class and returns the reference (or the indirect
pointer) to that instance. You need to store the reference returned by the
new
operator in a reference variable.
jack = new Human();
Note that
jack
itself is a variable and it will be allocated memory separately. The memory location for the
jack
variable will store the reference of the memory location of the newly created instance of the
Human
class. Figure
6-2
depicts the memory state when reference variable
jack
is declared and when an instance of the
Human
class is created
and its reference is assigned to the
jack
variable.
jack
jack
name
gender
X
X
Human jack;
jack = new Human( );
Figure 6-2.
Memory states when a reference variable is declared and when a reference variable is assigned the reference
of an instance of a class