• Post Reply 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static inner classes

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi All,

I have a basic question regarding the static nested class:

As per Ankit's article:

They can access non-static members of the enclosing class (including private non-static members) on an object of the enclosing class.
The reverse is also correct i.e. members of the enclosing class can access static members of the Member Class directly and non-static members of the Member Class on an Object of the Member Class.

And as per K & B inner classes chapter:

Just as a static method does not have access to the instance variables and nonstatic methods of the class, a static nested class does not have access to the instance
variables and nonstatic methods of the outer class. Look for static nested classes with code that behaves like a nonstatic (regular inner) class.

****************************

I agree to Ankit's article but not to excerpt from the K&B chapter.

Please correct me( i hope i am not missing something in between lines of K & B point )

Thanks
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

They can access non-static members of the enclosing class (including private non-static members) on an object of the enclosing class.



The operation "accessing" can be done in many different ways:
1) accessing a member variable by using it in a method of same class body, without an object qualifying the member variable.
2) accessing a member variable with a qualifying object. Eg:- myObject.myMemberVariable

3) ...and potentially a couple of more different ways.

The two authors you quoted meant different ways of access.
 
reji singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but suppose you have a question like this-

Which are true about a static nested class? (Choose One.)

1.It does not have access to non-static members of the enclosing class
2. They can access non-static members of the enclosing class (including private non-static members) on an object of the enclosing class.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Certainly 2. Because it clear and to the point.

It always better to check JLS or SUN tutorial if any of the article is misleading.

Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.


 
reply
    Bookmark Topic Watch Topic
  • New Topic