Reference Variables Objects Anonymous objects

Reference Variables Objects Anonymous objects

Reference Variables Anonymous objects


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.

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.

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?

The above statement can be split into two as follows.

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?

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.

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.

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?

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.

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)

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.

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.

39 thoughts on “Reference Variables Objects Anonymous objects

  1. Ajay

    Employee emp= new Employee();
    Employee emp1= new Employee();
    emp=new Employee();
    Employee emp2=new Employee();

    Assume, Employee is a valid class.
    Just tell me which objects are there in garbage collection.
    Explain in detail sir! Thanku.

  2. aves

    sir, creating object like demo b=new demo(); my java teacher told that ‘b’ is the reference of object. please explain me?.
    and what is the use of reference variable?

  3. Chinmay

    Suppose I have one abstract class A and i have another class B which extends the abstract class. I can create a reference variable of A and assign the object of B i.e
    A a=new B(); // This is sub-classed.
    Can you please explain this? And can object a access members of class A and B both? Can we do this for non-abstract class?

    1. S. Nageswara Rao, Corporate Trainer

      This works also with non-abstract (concrete) classes also. In your code observe the follwing:

      1. Object “a” calls A class methods when they are not overridden and calls B class methods when they are overridden.
      2. Observe this also. Do not override a method, that is, create a new method (which does not exist in A class) in B and call with object “a”. What happens?

  4. jagruth

    Hi sir,

    As you said that constructor creates anonymous object. when the object has a name,then new operator will allocates the memory for the object.

    my doubt is what happens in the below case

    new Demo()

    Will the new operator allocates the memory for anonymous object or not? (or)
    it will use temporary location.Once the anonymous object completes the assigned task the temporary location will be freed,

    and one more doubt is why the use of new operator in this case because anonymous object will be created my constructor . Here new operator won’t allocate the memory as no reference variable is assigned

  5. Souarv Dash

    This clears my all doubts that were before but i got a new one… here it is..
    Why the object of a class takes four byte of memory location when it is created? What I know is the object carries the address of the memory location. So is that for the addresses are in hexadecimal type and they occupy four bytes or it is for something else? Please explain…

  6. Abhinav Joshi

    Sir, Yesterday in an interview the interviewer asked me what is aliasing. But i was not able to answer. Now i got it but still didn’t get that what is the problem with aliasing. It’s like two objects pointing to same location.
    Please help.

    1. S. Nageswara Rao, Corporate Trainer

      You yourself is giving the answer.

      See Employee emp1 = new Employee();
      emp1.salary = 1000;\
      Employee emp1 = new Employee();
      emp2 = emp1;

      Now salary location is shared by two objects pointed by two objects. The salary location can be accessed by either emp1 and emp2. This is called alias or aliasing.

  7. yogesh vaidya

    thank u
    it is very helpfull for me . now all doubte related to objects ,instance and reference varible is cleare.
    please help me to in clear the doublt in throw and throws in exception handling in java.i m very confuse in these two

  8. phani

    plz explain these use of animal,croc(both are classes predefined)on the left hand side and wts the following expreesions depict??
    animal a1=new animal;
    croc c1=new animal;
    croc c2=new croc;

    1. Abhishek kumar singh

      In this question class type casting used
      beacuse Animal is super class or base class and croc is sub class so
      animal a1=new animal();
      croc c2=new croc();
      it shows normally how we create object;
      problem is in : croc c1=new animal();
      this is class type casting beacuse croc class inherit the animal class so
      so we make anonymus object of animal class and assign an object name of c1 which is of croc class

  9. phani

    I didnt get the concept clearly if v have reference variable like this; where animal and croc are classes;

    Animal a1=new animal;
    croc c1=new animal;
    croc c1=new croc;

Leave a Reply

Your email address will not be published. Required fields are marked *