Skip to main content
Lesson 12 - Object References
ZIPPDF (letter)
Lesson MenuPreviousNext
  
Object Reference variables page 4 of 10

  1. Since objects are big, complicated, and vary in size you do not automatically get an object when you declare an object reference variable. For example, in the declaration:

    String str;

    the variable str does not actually contain the object, but contains information about where the object is. An object reference is information on how to find a particular object. The object is a portion of main memory; a reference to the object serves as a way to get to that portion of memory.

  2. Here is a tiny program that declares a reference variable and then creates the object:

    public class StrRefExample
    {
      public static void main (String[] args)
      {
        String str;
        
        str = new String("example string");
        System.out.println(str);
      }
    }
  3. An object contains data and methods (attributes and behaviors). You can visualize the String object in the above program like this:

    The data section of the object contains the characters. The methods section of the object contains many methods.

  4. Objects are created while a program is running. Each object has a unique object reference, which is used to find it. When an object reference is assigned to a variable, then that variable says how to find that object.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.