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.
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.