File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing an object without a reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing an object without a reference" Watch "Accessing an object without a reference" New topic
Author

Accessing an object without a reference

adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

Hi,

I was going through the following piece of code



Is there any other way of accessing the Test object inside the inner class apart from the one mentioned in line #1, if i am creating the Test object from somewhere else and i haven't got a reference to it ?

Thanks,
Adithya.
Rituraj tyagi
Greenhorn

Joined: Oct 06, 2009
Posts: 13
Hi All

I'm also pursuing OCJP and gone through above problem and just extended adithya's example to clear my understanding as follows:-


And got this output :-

Test method invoked in main with innerTest as local from the inner private class
both instance equal

Now what I understand from above this that the line Test test = Test.this got the refrence of the class containing it . Please correct me if I'm wrong.
Thanks

Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 236

good work


SCJP 6- 91%, IBM DB2, IBM RAD Certified
Rituraj tyagi
Greenhorn

Joined: Oct 06, 2009
Posts: 13
Hi All I was going through the K & B again and found in 8 th chapter that this syntax is used to refer by inner class to reference the current instance of the outer class. refer page 669.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing an object without a reference
 
It's not a secret anymore!