Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor(pls reply me soon)

 
Manu Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as in the given below constructor it is given a compile time error
recurcive constructor invocation
public class B {
B(String s) {
this();
}
B() {
this("hai");
}
but in k&b it is given an exception
than what will do in exam either compiler error or exception
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i advice you to listen to the JAVA COMPILER.
i'll go for compile time error.
 
Alex Stojan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to compile the code and it gave me a compiler error (cyclic "this" constructor calls ...)
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could compile it successfuly. But throw StackOverflowError when i try to run it. Can someone explain what is the exact behaviour? I assume it is compiler dependant.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh which version of JDK are you using?
From the code you can recursion is occuring, with each constructor calling another without stopping.
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep,
am working on 1.4.0.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At some point creating an object of any class must resolve in calling super() of the superclass constructor. Remember, in any constructor your first statement can be either this(); or super(); calls. super(); is inserted implicitly by default only if this(); is not found. In the code above, there is a clear cyclic constructor invocation, I can't compile it with JDK 1.4.2.
As of Java 2 SDK 1.4.1, the compiler detects all cases of mutually recursive constructors. Previously, the compiler only detected a self-recursive constructor.

Hope, this link can help:
Enhancements and Changes in J2SE 1.4.1 Platform
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for the nice explanation Vad. Time for upgrade...huh...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic