File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing Outer Class variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing Outer Class variable" Watch "Accessing Outer Class variable" New topic

Accessing Outer Class variable

Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76

I thought this will compile and output 78 & 20 but i am getting a compile error saying variable y can't be accessed.
Correct me if i am wrong here, inner class should be able to access any outer class variable..including a private???

KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
In a bit of a hurry so don't have time to check, but at first sight try Outer.this.y
out is an instance of Outer.Inner; y is a variable of Outer..

Looluo Zhu

Joined: Nov 26, 2003
Posts: 8
You can access y in the body of Inner, not by a reference of Inner.

SCJP 1.4
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
The only way I was able to get at the value of Outer.y was by adding a method to the Inner class as indicated below:

I then changed the final line to be:

Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
Sorry, the previous post should read as:

I agree. Here's the link:
subject: Accessing Outer Class variable
It's not a secret anymore!