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 why private member variable of inner class can't be accessed by its outer class but mehod can be? 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)
Reply locked New topic
Author

why private member variable of inner class can't be accessed by its outer class but mehod can be?

Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
The following code is related to regular inner class and the moment i try to access private member variable of inner class compiler complains error but Not when private member is acessed
Here's the code



Anyone kindly explain this absurd behaviour .
Michael Labuschagne
Ranch Hand

Joined: May 08, 2007
Posts: 56

Divyya Joshi wrote:The following code is related to regular inner class and the moment i try to access private member variable of inner class compiler complains error but Not when private member is acessed
Here's the code



Anyone kindly explain this absurd behaviour .


It's not absurd behavior - on line 7 you have "private x=10;" you have forgotten to set the datatype ... int I presume? Where you have marked error the 'x' has not been declared properly because of what I said in the beginning.

Emre Yurtsever
Greenhorn

Joined: Aug 15, 2011
Posts: 1
Hi Divyya,

It seems that variable type is missing at line 7.

private int x=10;

With this little correction, the code compiles fine.

Hope this helps,
Emre.
Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
Oh yeah. Thanks . that was reaaly a very silly mistake of mine.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Double post of http://www.coderanch.com/t/549815/java-programmer-SCJP/certification/why-private-member-variable-inner , locking


luck, db
There are no new questions, but there may be new answers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: why private member variable of inner class can't be accessed by its outer class but mehod can be?
 
Similar Threads
Inner Classes Q
Regular Inner class
Inner Class question..
why private member variable of inner class can't be accessed by its outer class but mehod can be?
inner class instantiation from outside the the outer class