0

From what I have learned, the class objects in Java are actually references to those objects. An object itself cannot have a variable, only a reference to it.

Consider the following C++ example :

SomeClass A(5);
SomeClass &B = A;
SomeClass &C = B;

Now, I think I'm right in saying that all of the three statements below will use the exact same object :

A.someMethod(); //some object
B.someMethod(); //the same object
C.someMethod(); //the same object

However, in Java, although objects are actually references, using the assignment operator will create an entirely new object with a new reference to it.

SomeClass A = new SomeClass();
SomeClass B;
B = A;

Now, the method calls will call from entirely different objects :

A.someMethod(); //uses one object
B.someMethod(); //uses entirely different object

Please tell me whether I am right or wrong.

Vikram
  • 227
  • 2
  • 5
  • 14

3 Answers3

2

Java references are very similar to C++ references. Assigning one reference to another does not create a new object. In Java, new objects are created only when you explicitly use the new operator.

Addendum:

For completeness, I should mention that String objects follow their own rules. In particular, a String constant creates an object at compile time without an explicit use of new. Also, auto-(un)boxing, is a more advanced topic where objects are created without explicitly using new. However, the main point in both cases remains: assigning one reference to another does not create a new object. Both references refer to the same object.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 2
    A String constant will *never* create a new object. The String object that represents the string literal at runtime is created an interned BEFORE the code executes. By contrast, unboxing *may* cause objects to be created when the code is run ... but it doesn't always. – Stephen C Nov 27 '12 at 04:27
  • @StephenC Thanks for the clarification. I was trying to qualify my statement that objects are only created using `new` because some language Nazi would come along and tell me that literal String objects aren't created that way. I hope my updated answer is more accurate. – Code-Apprentice Nov 29 '12 at 00:11
  • 1
    "For completeness" - you should also mention that objects in Java can be created reflectively or by object deserialization, neither of which involve the `new` operator. – Stephen C Nov 30 '12 at 02:50
  • 1
    In fact, a better way to say what you are trying to say is to simply state that in Java neither a variable declaration or an assignment creates a new object. (On the other hand, assignment of a C++ reference / pointer typically doesn't create an object either. It possibly *could* create a new object, but only if you've done something a bit strange with operator overloading.) – Stephen C Nov 30 '12 at 02:54
2

References are different from objects. A particular object can have more than one reference pointing to it. When calling a method of a class at compile time always the class reference is checked whether the reference type contains the method defined in its class. If not a compile time error is issued. If the method is overridden in a subclass of the given class then the overridden method is called at run-time. At compile time always 'class-ref' in <class-ref>.method() is checked to see whether it contains the method definition. Other modifiers like static, final, abstract, method visibility are also checked at compile time itself and a compile time error is issued if the wrong combination of method modifiers is used.

    SomeClass A = new SomeClass();
    SomeClass B;
    B = A;        // Same class, same object, different reference


A.someMethod(); //uses one object
B.someMethod(); //uses the same object
clinton
  • 612
  • 3
  • 6
-2

Bro actually it seems that java is creating entirely different and new object when you make same references to an object (A=B). But in reality java will provide a copy of object to another reference which you have make like in your example A and B are referring to same object but they have their own copies of that object of type SomeClass. It is same as call by value thing of C language. You can use and modify that object, but modification made in that object will not effect original copy or other copy, means if you modify SomeClass object using reference B it will not affect object of reference B, although both are referring to same objects but they have their own copies.

Bilal
  • 1