• 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

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."
 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic