This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Hi Willamn, Because it is a static inner class, you are suppose to access it from outer class by using new Test1.A() or just new A(). Only then it will gives the answer, otherwise it throws exception. The reason why it throws exception if we use new Test1().new A(), i really don't know. Can any body explain this. What i feel is because new Test1 will creat a object of Test1(), through which we are accessing static class. Static class is accessed only by there class name and by instance of any other class. This is what I feel. Any body has any answer
Well, it doesn't compile for me either, and I'm on Windows NT with jdk1.3.0_02. The problem is that you can't refer to a static class the same way you refer to a non-static class. Static classes are ONLY allowed inside other top-level classes or interfaces. In fact, when you create a static class, it's not *really* a member of that class...at least you shouldn't think of it that way. It's certainly NOT an *inner class*, even though it is a nested class. You refer to a static class via it's fully qualified name, which includes all the names of any classes it is enclosed by. In this case, it's only enclosed by one class, Test1, so class A's fully qualified class name is Test1.A (obviously if Test1 were nested within a package, that package name would be part of class A's fully qualified name). Since class A has a default access modifier, it can be refered to by any class in the same package as Test1. If I had another class, Foo, in the same package as Test1, I would refer to class A as: Test1.A If I want to create a new instance of class A, I write new Test1.A(); Think of static nested classes as just a way to extend the package naming heirarchy. Since class A is defined within Test1, if I want to create a new instance of this class in a method of Test1, such as main above, I could simply write new A() because class A is visible in that scope. What you can't do is what you tried to do above, that is, create an instance of class A via an instance of Test1. new Test1().new A() //bad, doesn't work. This kind of syntax would only work if A was an inner class, but it's NOT, it's a static nested class. Hope this clears it up! Rob
It doesn't compile for me either and it realy shouldn't. The expression in question here is: A a = new Test1().new A(); The entire right hand side is a qualified class instance creation expression. In this type of instance creation expression it is not allowed for the class being created to be a static class. For the real straight stroy check out the JLS Section 15.9.1 Here is a quote where it is talking about instance creation expressions using qualified expressions.
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.
In this case Primary is the class Test1 and Identifier is the 'A()' part of the expression. Since A is defined as static it isn't an inner class and should throw an exception when compiled. For more info check out the current edition of the JavaRanch newsletter, there's an article in it where I tried to explain this part of the JLS and make it simpler to understand - I think I picked a rather dry topic though hope this helped
Joined: Jan 07, 2002
It doesn't compile for me. Even if it did, it's bad form to refer to static members via an instance reference. Static members aren't inherited like instance members, especially when overriding them, so if a subclass hides a static class by defining a new version in a subclass, you are asking for trouble when you try to instantiate it via an instance reference. I can't compile this code, but see if you can and what results you get:
Since a static class exists without reference to a particular object instance, it leads to confusion to refer to it via an instance reference. Rob [ January 16, 2002: Message edited by: Rob Ross ]