• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

is it wrong?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class OuterClass{
private double d1=1.0;
static class InnerOne{
double methoda(){
return new OuterClass().d1; //is it wrong?
}
}
public static void main(String args[]){
InnerOne myclass=new InnerOne();
System.out.println(myclass.methoda());
}
}
the result: it can run and get the answer 1.0
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is perfectly fine since the inner class can access all the private members of the outer class.
 
pony
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but as the Student Guide of SL-275 page 7-36 said:
"Inner classes that are declared static automatically became
top-level classes"
so the "Inner class" now isn't Inner class is as same level
class as Outer,I think it can't visit private varibles of
Outer class.

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static inner classes are correctly called as top-level nested classes or simply nested classes. So, they are nested. Nested classes can access the member variables of the enclosing class.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it can't "visit" the private variables like you say, then your first example obviously wouldn't work. So you're wrong. In Bill Brogden's Exam Cram page 82 on the topic of static inner classes also known as nested classes, and I quote ..." Just as member methods in a class have unlimited access to all private and other variables and methods in the class, nested classes also have unlimited access."
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic