File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor(pls reply me soon) 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 "constructor(pls reply me soon)" Watch "constructor(pls reply me soon)" New topic

constructor(pls reply me soon)

Manu Kumar

Joined: Aug 31, 2003
Posts: 8
as in the given below constructor it is given a compile time error
recurcive constructor invocation
public class B {
B(String s) {
B() {
but in k&b it is given an exception
than what will do in exam either compiler error or exception

SCPJ2 aspirant
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
i advice you to listen to the JAVA COMPILER.
i'll go for compile time error.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Alex Stojan

Joined: Sep 19, 2003
Posts: 4
I tried to compile the code and it gave me a compiler error (cyclic "this" constructor calls ...)
Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152
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.

SCJP 1.6
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

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

Joined: Sep 24, 2003
Posts: 152
am working on 1.4.0.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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

Joined: Sep 24, 2003
Posts: 152
Thanx for the nice explanation Vad. Time for upgrade...huh...
I agree. Here's the link:
subject: constructor(pls reply me soon)
It's not a secret anymore!