• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

K&B Book Constructor issue

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was looking into the second chapter in K&B book and found something which seems to be practically not happening.

The book says:
Constructor can have this() and in case if none of the overloaded constructors in the class have a super() the stack explodes.

Below is a sample example:
public class ConstructorDemo{

public ConstructorDemo(){
this();
System.out.println("Hai");

}

public static void main(String []args){

new ConstructorDemo();
}

}
It doesnt compile at all. I get an error saying recursive constructor invocation.

Is this a possible error in the book Bert?
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A contructor can call another constructor first to start the contruction of an object. In your case, you contructor is calling itself. The compiler is trying to tell you that this operation will never finish -- endless.

Henry
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I see what you are trying to ask... Try rewriting your class to have two different constructors that call each other.

I would be interested to see if it compiles. And if it does, you should get a stack overflow trying to instantiate it.

Henry
 
Jitender kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ufffff...I tried it..

public class ConstructorDemo{

public ConstructorDemo(){
this(1);
System.out.println("no args");
}

public ConstructorDemo(int a){

this();
System.out.println("one arg");
}

public static void main(String []a)
{
new ConstructorDemo();
}

}

Got the same error though...did not compile at all.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like an error in the book. Perhaps what was meant is that, if this weren't detected as an error at compile time, the stack would certainly overflow. In fact, although this sort of thing has always been considered a compile-time error according to the JLS, JDKs prior to 1.4 did not always detect it correctly. I believe they generally detected a constructor calling itself directly, but they didn't detect circular chains. If the compiler fails to detect the problem, you get a stack overflow error. Since JDK 1.4 however, this shouldn't be an issue. (Unless some new bug emerges in the future.)

For purposes of the exam (if there's a question like this; dunno), the correct answer would be that it causes a compile-time error.
[ February 24, 2006: Message edited by: Jim Yingst ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
... Perhaps what was meant is that, if this weren't detected as an error at compile time, the stack would certainly overflow...


That's my interpretation. I tried to get an overflow with a tangled web of constructors going around in circles calling each other, but the compiler was "smart enough" to catch it.
[ February 24, 2006: Message edited by: marc weber ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:

That's my interpretation. I tried to get an overflow with a tangled web of constructors going around in circles calling each other, but the compiler was "smart enough" to catch it.

[ February 24, 2006: Message edited by: marc weber ]



Maybe I'm overlooking something but it seems to me that the compiler doesn't have to be particular smart to catch this error. I.e. it just needs to find atleast 1 ctor in which there is either an explicit super call or in which a super call can be inserted. No need to follow a web, just check and check off each ctor for super() appearances. If the count is 0 after looking at each ctor there must be some recursion going on.

Anyway, I would appreciate it if Bert could respond to this issue. The way he writes about it makes it look like this is a compiler implementation dependent case, but if the Java spec actually requires this to be a compilaton error we're talking about something entirely else.

(btw, I just checked with JDT from Eclipse 3.1 and it also gives the "Recursive Contructor Innovation" message.)
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Saskia]: I.e. it just needs to find atleast 1 ctor in which there is either an explicit super call or in which a super call can be inserted.

No, that wouldn't be sufficient. For example:

Here the first constructor would work just fine, implicitly invoking super(). However the other two constructors have circular invocations, which will not compile with a modern compiler.

[Sakia]: The way he writes about it makes it look like this is a compiler implementation dependent case, but if the Java spec actually requires this to be a compilaton error we're talking about something entirely else.

Well, JLS3 8.8.7 is unambiguous here: "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."
[ February 26, 2006: Message edited by: Jim Yingst ]
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic