File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it wrong?

 
pony
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sean Casey
Ranch Hand
Posts: 625
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
natarajan meghanathan
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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."
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic