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
I like Java
Sean Casey
Ranch Hand
Joined: Dec 16, 2000
Posts: 625
posted
0
This is perfectly fine since the inner class can access all the private members of the outer class.
pony
Greenhorn
Joined: Jan 22, 2001
Posts: 6
posted
0
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
Joined: Feb 01, 2001
Posts: 130
posted
0
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.
Sun Certified Programmer for Java 2 Platform
Sean Casey
Ranch Hand
Joined: Dec 16, 2000
Posts: 625
posted
0
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."