• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner Classes vs. Static Nested Class access to members? Clarification please.  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Oracle says

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)


Can someone please clarify this for me?  I thought a "member" is a variable declared in the class level.  However my declared variable was accessible from my static nested class.    I declared a private int variable  ( private int numeric = 0; ) in my class file and my static nested class was able to access it.

Thank You



 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What they meant is that something like this is illegal:
A static nested class can't access members of an enclosing class. This is analogous to a static method not being able to access those members.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:What they meant is that something like this is illegal:
A static nested class can't access members of an enclosing class. This is analogous to a static method not being able to access those members.


Oh!  Okay.   Thank You!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa Austin wrote:I thought a "member" is a variable declared in the class level.

A class member can be other things, too.  See the JLS definition: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.6

"A class body may contain declarations of members of the class, that is, fields (§8.3), methods (§8.4), classes (§8.5), and interfaces (§8.5)."

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Lisa Austin wrote:I thought a "member" is a variable declared in the class level.

A class member can be other things, too.  See the JLS definition: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.6

"A class body may contain declarations of members of the class, that is, fields (§8.3), methods (§8.4), classes (§8.5), and interfaces (§8.5)."



Thank You!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!