Class Type Variables Storing References

This section describes how references of class objects are stored in class variables of the same class type.

When a variable is declared as a class type, it will be used to store references of objects of the same class type. The object class type must match the variable class type. Examples are listed below:

   String msg = new String("Hello"); 
      // assigns the reference of a String object

   java.util.Date today = new java.util.Date();
      // assigns the reference of a Data object

   java.util.ArrayList<String> 
      = new java.util.ArrayList<String>(); 
      // assigns the reference of an ArrayList<String> object

Since class type variables stores references of class objects, it is very common that the reference of a single object can be stored in multiple variables. Here is a tutorial example program that shows you how the reference of a single object is stored in to variables:

/* ClassTypeVariable.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
class ClassTypeVariable {
   public static void main(String[] a) {
      java.io.PrintStream o = System.out;

      // assigning the reference of a Date object
      Date myTime = new Date();

      // showing the object content
      o.println("myTime before = "+myTime);

      // assigning the reference to another variable
      Date hisTime = myTime;

      // modifying the object content
      hisTime.setTime(hisTime.getTime()+1000*60*60);

      // showing the object content
      o.println("myTime after = "+myTime);
      o.println("hisTime after = "+hisTime);
   }
}

Compile and run the example, you will get:

myTime before = Sun May 04 22:14:47 PDT 2014
myTime after = Sun May 04 23:14:47 PDT 2014
hisTime after = Sun May 04 23:14:47 PDT 2014

The result proves that both "myTime" and "hisTime" are references of the same object. When the object value is modified, time moved to 1 hour later, through "hisTime" reference, "myTime" reference can also see the change.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

Reference Data Types and Variables

 Reference Types Supported in Java

 Creating Class Type Objects

Class Type Variables Storing References

 Interface Type Variables

 Class and Interface Hierarchy

 Supertype and Subtype

 Explicit and Implicit Type Casting

 Type Casting Example Program

 Type Casting Compile and Runtime Error

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version