Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 56
Tomcat Server Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Oh yeah. Thanks . that was reaaly a very silly mistake of mine.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Double post of https://coderanch.com/t/549815/java-programmer-SCJP/certification/why-private-member-variable-inner , locking
 
I think I'll just lie down here for a second. And ponder this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
    Bookmark Topic Watch Topic
  • New Topic