• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler doesn't complain about the code.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic