SCJP
Visit my download page
Regards,<BR>Shree
Originally posted by Sam Wong:
Take the following example:
<pre>class Outer {
class Inner{}
public static void main(String[] args) {
Inner a = new Outer().new Inner(); //1
Outer.Inner f = new Outer().new Inner();
}
public void method() {
Inner b = new Inner();
Inner c = this.new Inner();
Outer.Inner d = new Inner(); //2
Outer.Inner e = new Outer().new Inner(); //3
}
}
public class Separate {
public static void main(String[] args) {
Outer.Inner b = new Outer().new Inner();
/** Inner g = new Outer().new Inner(); // this is invalid */
}
}
</pre>
Answer depends on where you're trying to create the inner class. The above code demonstrate some of the valid construction calls.
Regards,<BR>Shree
Originally posted by Sam Wong:
Now, my doubts,
1.In the context of rule 3, isn't line marked //1 an error?
Inner a = new Outer().new Inner();
[b]It is valid because this static method resides in the enclosing class. So it doesn't have to reference the full namespace. It knows about Inner. Similar to the fact that you don't need to import other classes in your own package to reference them without the full package name.
[/B]
Regards,<BR>Shree
Regards,<BR>Shree
I want my playground back. Here, I'll give you this tiny ad for it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|