This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access an object via reference variable (or not) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access an object via reference variable (or not)" Watch "Access an object via reference variable (or not)" New topic
Author

Access an object via reference variable (or not)

Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
Hi. Hope the qustion is not too silly, but I prefer to know if I have missed something along the way. Chapter on polymorphism (KB page 95):
Remember that the only way to access an object is through a reference variable...


If it is true what the book says and if it's correct that:


I'm wondering why the following code compiles and runs if Line 1 is not using a reference variable to access the object.



Thanks.


SCJP 5
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
This is a little tricky. The point is that the result of an instance creation expression is an anonymous reference of the same type as the class being created (read the JLS if you don't believe me )

So:

String s = new B().bMethod(); // Result of new B() is a reference of type B

There is always a reference involved, whether explicitly coded, or implicit.

I hope that helps to clear your doubts.


All code in my posts, unless a source is explicitly mentioned, is my own.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
new NoRefTest() ==> Only a new object created, that can be referenced by reference variable.

new NoRefTest().isValid(); ==> new object is created and then a method of it is called in the same line. The method returns a boolean value that you cannot put in a reference variable, but in a boolean variable.

SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
Ah, the (in)famous implicit reference, then I guessed well...

Thank you very much!
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
How do I delete my post ..dont see the option any more
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Paolo Dina wrote:Ah, the (in)famous implicit reference, then I guessed well...

Thank you very much!

You are welcome, Paolo. I also wondered about this at some point. Then checked the JLS and all the doubts were gone.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Himalay Majumdar wrote:How do I delete my post ..dont see the option any more

Unfortunately that option is gone (but will hopefully resurface at some point.) For now, you are left with the option of editing your post and deleting its content (replace it by something like: Deleted.)
 
Consider Paul's rocket mass heater.
 
subject: Access an object via reference variable (or not)
 
Similar Threads
Properties/Settings - a design question
Class and Object Problem
Casting
equals();
calling the right variable and method