GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question" Watch "question" New topic
Author

question

bill1111
Greenhorn

Joined: Jan 15, 2002
Posts: 5
why is such an error msg when run this code?
class Test1{
static class A{
}
public static void main(String[] args){
A a = new Test1().new A();
System.out.println("a = " + a);
}
}

err msg is
Exception in thread "main" java.lang.VerifyError: (class: Test1, method: main signature: ([Ljava/lang/String V) Expecting to find unitialized object on stack
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It runs fine on my machine... The output is
a = Test$A@310d42
HIH
Moreover, we'd like you to read Javaranch Naming Policy and register again.
Thank you for your cooperation


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
bill william
Greenhorn

Joined: Jan 15, 2002
Posts: 1
thanks. I just re-registered.
I ran it on Solaris 8 and java 1.2.2. still the same result. what happened. did you notice that tehre is a "static" before class A? thanks
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
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


Arathi<br />Sun Certified Java Programmer
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Hi.

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


Rob
SCJP 1.4
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Compiler doesn't complain about the code.


SCJP2. Please Indent your code using UBB Code
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Jose,
what do you mean? Do you mean *your* compiler isn't giving you an error? If so , which version do you have? Your message wasn't very helpful!!
Rob
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It's working on my side too:
output is a = Test$A@310d42
Java 1.3.1_01 on Linux Redhat 7.1 kernel 2.4.3.-12
HIH
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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


Dave
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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 ]
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Very odd that it is compiling for some and not others. It compiles fine for me Windows2000 jdk1.3.1_02.
Both ways seem to work:

[ January 16, 2002: Message edited by: Rick Reumann ]
sylvia weller
Greenhorn

Joined: Jan 07, 2002
Posts: 29
The original example compiled and ran fine under jdk1.3 on Windows 95 (only have access to Windows 95 right now - how embarrassing :roll: ). The output was
"a = Test1$A@111f&1"
Sylvia
 
GeeCON Prague 2014
 
subject: question