• Post Reply Bookmark Topic Watch Topic
  • New Topic

In Java private fields are not really private on inner classes.  RSS feed

 
Abdul Aatif
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one explain this ,
i have seen in a post here





Thank you in advance
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just how the Java programming language was designed - private fields in inner classes are accessible from the outer class.

The page you linked to on StackOverflow already has some answers. What more would you like to know?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And if anyone is interested -- the wording from the Java Language Specification...

if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.



In other words, "private" means "private to the top level class". Anything declared as private in the body of the top level class (including inner classes obviously) can be accessed from code in the body of the top level class (including code in the top level, or even a different inner class)

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!