Java Reference Types, Constructing and Initializing Objects


Java Reference Types

The variable today is a reference variable holding one MyDate object. As you have seen, there are eight primitive Java types: boolean, char, byte, short, int, long, float, and double. All other types refer to objects rather than primitives. Variables that refer to objects are reference variables. For example, you can define a class MyDate:

1 public class MyDate {

2 private int day = 1;

3 private int month = 1;

4 private int year = 2000;

5 public MyDate(int day, int month, int year) { … }

6 public String toString() { … }

7 }

The following is an example of using MyDate:

1 public class TestMyDate {

2 public static void main(String[] args) {

3 MyDate today = new MyDate(22, 7, 1964);

4 }

5 }

The variable today is a referece holding one MyDate object.

Constructing and Initializing Objects

You have seen how you must execute a call to new Xyz() to allocate space for a new object. You will see that sometimes you can place arguments in the parentheses, for example

new MyDate(22, 7, 1964).

Using the keyword new causes the following:

1. First, the space for the new object is allocated and initialized to the form of 0 or null. In the Java programming language, this phase is indivisible to ensure that you cannot have an object with random values in it.

2. Second, any explicit initialization is performed.

3. Third, a constructor, which is a special method, is executed. Arguments passed in the parentheses to new are passed to the constructor (22, 7, 1964).

4. Finally, the return value from the new operation is a reference to the new object in heap memory. This reference is stored in the reference variable.

Memory Allocation and Layout

In a method body, the following declaration allocates storage only for the reference shown in Figure


The keyword new in the following example implies allocation and initialization of storage, as shown in Figure.


Explicit Attribute Initialization

If you place simple assignment expressions in your member declarations, you can initialize members explicitly during construction of your object. In the MyDate class in this example, initializing all three attributes explicitly is declared, as shown in Figure:


Executing the Constructor

The final stage of initializing a new object is to call the constructor. The constructor enables you to override the default initialization. You can perform computations. You can also pass arguments into the construction process so that the code that requests the construction of the new object can control the object it creates.

The following example calls the constructor, as shown in Figure:


Assigning a Variable

The variable assignment then sets up the reference variable my_birth in this example, so that it refers properly to the newly created object as shown in Figure.


Assigning References

In the Java programming language, a variable declared with a type of class is referred to as a reference type because it refers to a non-primitive type. This has consequences for the meaning of the assignment. Consider this code fragment:

int x = 7;

int y = x;

MyDate s = new MyDate(22, 7, 1964);

MyDate t = s;

Four variables are created: two primitives of type int and two references of type MyDate. The value of x is 7, and this value is copied into y. Both x and y are independent variables, and further changes to either do not affect the other. With the variables s and t, only one MyDate object exists, and it contains the date 22 July 1964. Both s and t refer to that single object, as shown in Figure.


With a reassignment of the variable t, the new MyDate object (for 22 December 1964) is created and t refers to this object, as shown in Figure. This scenario is depicted as: