Bookmark Topic Watch Topic
  • New Topic

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

 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 56
Java Tomcat Server Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Report post to moderator
Oh yeah. Thanks . that was reaaly a very silly mistake of mine.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
Double post of http://www.coderanch.com/t/549815/java-programmer-SCJP/certification/why-private-member-variable-inner , locking
 
    Bookmark Topic Watch Topic
  • New Topic