Ok, I admit, I'm using an older version 1.2.2. That's all I got at work. But I've got 1.4.2 installed on my PC at home and will go back and verify the results in a few hrs. But I trust Cathy & Vad on that. Cheers Harwinder
When referring to a static class whether it be top level or nested, you cannot use an instance variable to get to it. For example, if you want to use a method from the Math class, you simply use Math.<method name>. You do not instantiate a static class. Same rule applies here even though it is nested. Use the fully-qualified class name to refer to the variable in the static class: OuterReferVarOfStaticInner.StaticInner.i
Hi Chris, Math class is NOT static. And top-level classes MAY NOT be static. The reason you cant create an instance of Math is because its constructor is private. You'll get the following error: "Math() has private access in java.lang.Math" The reason you can access the methods of Math class without creating an instance of Math is because the *methods* are static, that means thay are NOT associated with an instance of the class, rather the class itself. That is why static methods are also called class methods.
With Java 1.4.2_02, I get the same error message as Vad mentioned above. Btw, you could use: int j = new OuterReferVarOfStaticInner.StaticInner().i; //line 3 ok OR int j = new StaticInner().i; //line 4 ok Cheers Harwinder [ November 12, 2003: Message edited by: Harwinder Bhatia ]
All I have read is that for a static nested class, you don't need an instance of the enclosing class. But, I've "not" read anywhere that you absolutely 'cannot' (compile-time error) specify an instance of the outer class. Could somebody please clarify? Thanks Harwinder
Welcome to the Ranch Pallavi, Chris and Dan. __________________________________________________________________ This is from JLS 15.9
ClassInstanceCreationExpression: new ClassOrInterfaceType ( ArgumentListopt ) ClassBodyopt Primary.new Identifier ( ArgumentListopt ) ClassBodyopt Class instance creation expressions have two forms: * Unqualified class instance creation expressions begin with the keyword new. An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top-level (�7.6), member (�8.5, �9.5), local (�14.3) or anonymous class (�15.9.5). * Qualified class instance creation expressions begin with a Primary. A qualified class instance creation expression enables the creation of instances of inner member classes and their anonymous subclasses.
And from JLS 15.9.1
Otherwise, the class instance creation expression is a qualified class instance creation expression. It is a compile-time error if Identifier is not the simple name (�6.2) of an accessible (�6.6) non-abstract inner class (�8.1.2) T that is a member of the compile-time type of the Primary. It is also a compile-time error if Identifier is ambiguous (�8.5). The class being instantiated is the class denoted by Identifier.
Thus it seems that the compiler is getting more and more JLS compliant. [ November 12, 2003: Message edited by: Jose Botella ]