Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

possible java bug

 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was doing some constructor exercises and did this piece of code:
public class Test {
public static void main(String args[]) {
Test t = new Test();
}
public Test() {
this(5);
System.out.println("Test()");
}
public Test(int i) {
this();
System.out.println("Test(int)");
}
}
jikes complain about it saiyng "The constructor "Test" may not directly or indirectly invoke itself". But javac 1.3 and 1.4 compiles without error. Is this a bug from javac ? Or is it supposed to overlook this?
Thanks,
Francisco
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS doesn't actually say this is illegal, so in one sense it's a "bug" in jikes to forbid this. However it's obvious that this code can never actually execute, so jikes is doing you a favor by complaining at compile time. SDK 1.3 and 1.4 will wait until run time to fail, with this code.
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that�s exactly what happens, when I try to run it, I get a "StackOverflowError". Should I write an email to Sun? or do you think they are aware of this?
I looked on java�s bug parade and found nothing on this.what�s your sugestion?
[]�s
Francisco
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francisco and Jim:
From the JLS section 8.8.5:
It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this.
So it might be a bug in the compiler. Although I want to think I've read something about it here once before, you might want to do a search for it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch, Dave. I was actually surprised when I couldn't find a place in the JLS forbidding it - I was looking in 8.8.5.1. Should have looked up the page a tiny bit. So yes, it is a bug in Sun's SDK's. This fits the general pattern though, which is that jikes is better about following the JLS than Sun is. I'd file a bug report, sure, but I doubt Sun's going to be in any rush to fix this one. Practically speaking, it doesn't really matter most of the time - anyone trying to do something like this will find out soon enough that it can't be done. It will probably be viewed as fairly minor, from Sun's perspective.
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help you both...I think i�ll search the saloon like you said Dave, and if I don�t find anything, I�ll post the bug on Sun�s site.
Thanks again,
Francisco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic