• 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

constructor(pls reply me soon)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i advice you to listen to the JAVA COMPILER.
i'll go for compile time error.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile the code and it gave me a compiler error (cyclic "this" constructor calls ...)
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep,
am working on 1.4.0.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx for the nice explanation Vad. Time for upgrade...huh...
reply
    Bookmark Topic Watch Topic
  • New Topic