Way2Java

Reference Variables Objects Anonymous objects

Reference Variables Anonymous objects

Summary:

By the end of this tutorial "Reference Variables Anonymous objects", you will be comfortable in creation of Java objects, reference variables and anonymous objects.

Java includes two types of variables – primitive variables and reference variables. A primitive type refers a primitive data type and reference type refers a class.

Observe the following which you are well acquainted.

public class Demo
{
  int x = 10;
  public static void main(String args[])
  {
    Demo d1;
    // System.out.println(d1.x);

    d1 = new Demo();
    System.out.println(d1.x);
  }
}

We have created and used many objects in all the previous programs. Now let us analyze the code of creating an object.

The general way of creating an object is of the following format.

       Demo d1 = new Demo();

In the above statement, we say, object d1 of Demo class is instantiated.

1. What is an object and instance?

Generally a C++ programmer says immediately, "an instance of a class is known as object". Of course, he is very right but the statement will be very confusing to a novice. Let us be clearer through some common day-to-day example. We cut a tree and get the wood. How to make use of the wood? Wood itself is waste as we cannot use it straight away. The wood should be converted into objects like door, table and duster and then used. Without these objects, simply, wood is waste. We say door, table and duster are the instances of wood. Door is an object of wood or we can say, the door is an instance of wood.

Similarly we have a class containing constructors, variables and methods (all these are known as constructs of a class). How to make use of them? We require objects of the class. Using these objects we can access the contents of the class. The object is d1. With d1, all the contents of the class can be made use of. Now we can say, the instance of a class is object. For this reason object creation is known as instantiation. No object, class is simply waste (like no door, wood is waste).

2. What is reference variable?
       Demo d1 = new Demo();

The above statement can be split into two as follows.

 
       Demo d1;                        //  1st
       d1 = new Demo();                // 2nd

In the first statement, d1 is called a reference variable (not object). A variable that refers a class name is known as reference variable. A reference variable cannot work like an object until it is converted into an object. In the second statement, reference variable is converted into an object.

3. Who has converted the reference variable into an object? OR
4. What constructor returns?
       d1 = new Demo(); 

There are two words on the right side; new and Demo(). Demo() is known as constructor. What a constructor returns? Generally, it is said that constructor does not return a value, but still we do not write void. Why, because the constructor really returns something. The constructor returns an object of a class known as "class object". This "class object" does not have a name. The nameless object in Java is known as "anonymous object". That is, a constructor returns an anonymous object. The anonymous object (returned by the constructor) is assigned to the reference variable d1. Now d1 is called an object. When object has a name, the new operator allocates memory for the d1 object. Any object in Java takes four bytes of memory. Now d1 object exists in the memory and can be used throughout the program to call a method or variable, any number of times.

5. How can we use reference variables in a program?

Just we came to know, a reference variable cannot work like an object until it is converted. But still, reference variables can be used to assign with an object. Observe the following.

    Demo d1 = new Demo();       // an object
    Demo d2, d3;                // reference variables

    System.out.println(d1.x);   // prints 10
    d2 = d1;                    // an object is assigned to a reference variable
    System.out.println(d2.x);       
                                // prints 10 as now reference variable works like an object

    d3 = d2;                    // reference to reference
    System.out.println(d3.x);   // prints 10

Now even through d2 and d3 are reference variables but still they are used as objects to call x value. What happened? How they print? When assigned, d1, d2 and d3 refer the same location of x. What is the proof? See the following code.

   d2.x = 20;
   System.out.println(d1.x);       // prints 20
   System.out.println(d2.x);       // prints 20
   System.out.println(d3.x);       // prints 20

All prints 20 as all point to the same location. If one changes, others also gets affected.; no encapsulation.

6. How anonymous objects are useful in the program?
 
       d1 = new Demo();   

In the above statement, the right-side expression, new Demo(), is known as anonymous object. If an anonymous object is not assigned a value, like d1, what happens to the anonymous object.

       new Demo();

In the above statement, d1 is not assigned. The anonymous object is created and dies instantaneously. But, still with anonymous objects work can be extracted before it dies as follows.

Example on the usage of Anonymous objects (of Reference Variables Anonymous objects)
public class Demo
{
  int x = 10;
  public void display()
  {
    System.out.println("Hello 1");
  }
  public static void main(String args[])
  {
    System.out.println(new Demo().x);
    System.out.println(new Demo().x);
    System.out.println(new Demo().x);
    new Demo().display();
    new Demo().display();
  }
}

As you can observe in the code, the anonymous object can be used only once in the program. We can’t use twice or more as the anonymous object dies immediately after doing its assigned task (here printing x and calling display()). Java designers advice to use anonymous objects when the object is required only once in the program. The advantage is to save memory, as anonymous object dies immediately. In I/O streams and AWT, we use many objects only once in the program; for them, better go for anonymous objects as follows.

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 g.setColor(new Color(100, 200, 80));

In the above two statements, anonymous objects of InpputStreamReader and Color are used.

Java also supports anonymous inner class objects; we used this in window closing.