Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Can anybody explain the error msg when run?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>
class Tree{
public Tree() {
System.out.println("hi");
}
public static void main(String args[]){
Tree t=new Tree();
Tree.Tree1 t1=new Tree().new Tree1( );//compiles..surprise.%#%$
t.amethod();//works perfect sepeartely//
t1.amethod();//compiles..runtime exception//
}
public void amethod(){
System.out.println("Hi method 1");
}
public static class Tree1{
public void amethod(){
System.out.println("from inner");
}
}
}
</pre>

[This message has been edited by J Maruti (edited February 02, 2000).]
 
Trailboss
Posts: 23083
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm gonna move this thread to the Java in General forum where it can get more attention.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have seen that before too (so you're not crazy!
And I have no good answer except for the one that you already know:
use the right idiom to instantiate a member of the top-level nested class,
<CODE>Tree.Tree1 t1=new Tree.Tree1()</CODE>
instead...
Oh well!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dunno Tony, it's possible you're both crazy. As for me, I'm quite lost here. The code presented does not compile, as it is missing two }'s. I made guesses as to where they should go - one on the fifth line after main(), and one at the very end - and everything compiles and runs fine using jdk 1.3. So, what is actually the question? I agree with Tony that "new Tree().new Tree1()" is better as "new Tree.Tree1()" - but that shouldn't (and doesn't) cause an error. So - what code are you actually using, and what error exactly are you getting?
Also, if you repost or re-edit the code - please use the [ code ] tag (no spaces) and indent the code to make it readable. Read about it here. Thanks.

[This message has been edited by Jim Yingst (edited February 01, 2000).]
 
J Maruti
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i am talking about the error msg that appears after u get the code to compile.
by mistake i posted the wrong code.
I used the code tag but may be there was a spelling mistake.
i will surely check when i post again.

[This message has been edited by J Maruti (edited February 02, 2000).]
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JDK 1.2.2 if you try to instantiate the static inner class using two "new", the code compiles but crashes on execution. My test code is gone (it's volatile) but that is essentially it. Try your code with 1.2.2 and you will see.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Since I don't have JDK 1.3 BETA, I can't confirm the this is a valid nested top-level class instantiation:
(it compiles but does not run):
<PRE>
class X {
static class Y{}
}
</PRE>
Now, is this legal:
X.Y xy = new X().new (Y);
as is
X.Y xy = new X.Y();
?
If so, it would be like calling static methods (or vars) using an object reference instead of class name? Is that it?
If so, how many mock tests would have the wrong answer?
Thanks.
 
J Maruti
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
when i was preparing for my certification, i remember writing a number of sample program in order to understand some of the features.
i do remember testing this <code>new XX().new YY();</code>
i never had any problem. now i am using 1.3beta. maybe that is the reason why i got the errors.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony- All are legal, compiling and running fine in 1.3 beta. Well,
<code><pre> X.Y xy = new X().new (Y);</pre></code>
needs to be
<code><pre> X.Y xy = new X().new Y();</pre></code>
but I knew what you meant. Yes, this seems to be just like calling static methods using an instance rather than a class name. I'd be mildly surprised if any mock (programmer) exams even asked a question about this, and very surprised if an actual Sun exam did so. I have no idea what their answer would be if they did, though.

[This message has been edited by Jim Yingst (edited February 02, 2000).]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Maruti-
I think it's the reverse. Your code (thanks for re-editing it) compiles under both 1.2.2 and 1.3, but it unly runs successfully under 1.3. With 1.2.2 I get the error:
<code>Exception in thread "main" java.lang.VerifyError: (class: Tree, method: main signature: ([Ljava/lang/String V) Expecting to find unitialized object on stack</code>
I really don't know what this means, and I regard it as a bug in 1.2.2. For what it's worth, although it shows up as a run-time error, it really depends on which jdk is used to compile:
Compile with 1.2.2, run with 1.2.2 --> error
Compile with 1.2.2, run with 1.3 --> error
Compile with 1.3, run with 1.2.2 --> no error
Compile with 1.3, run with 1.3 --> no error
An aside: this sort of test is useful for those of us developing applications using 1.3 that might be run on other JVM's. I can control what I compile with, but not what others may run it on. (Also we can't legally ship jre 1.3 beta with our product under its current license, but we can with jre 1.2.2. That will probably change once 1.3 leaves beta, but until then...
 
This one time, at bandcamp, I had relations with a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!