• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Constructor related Q from Dan's Test

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which of the following statements are true?
a. A constructor can invoke another constructor of the same class using the constructor invocation statement "this".
b. A constructor can invoke itself using the constructor invocation statement "this".
c. The constructor invocation statement, "this", can legally appear anywhere in the constructor body.
d. A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation statement "super".
e. The number of constructor invocation statements that may appear in any constructor body can equal but not exceed the number of alternate constructors declared in the same class.
f. A constructor is not permitted to throw an exception.
g. None of the above.


The correct answers are A and D
But my doubt is related to option B that is given. The answer given to justify B is :
"A compile-time error is generated if a constructor attempts to invoke itself either directly or indirectly"
I had also marked it true, but later found out that the compiler cries out foul if I have code like this.

But if I comment this() statement at line marked 1, and uncoment the next line, i.e new A(), the thing does not create a compiler error, though and infinite loop is create and the system soon runs out of stack.
Can anybody please provide a justification for this behaviour. Why is new A() allowed and not this() ??
Rgds,
Anupreet
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The main purpose of a constructor is to initialize the state of the
object. It makes sence to not allow recursion to this type of work
as one want to initialze the state only once at the beginning.
So j2sdk1.4 compiler reports this as
On the other hand will create a new Object
by calling the same constructor it is in. And it goes on creating
the objects and filling up the stack.
-A
 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
convincing argument Allan.. thanks
I saw where I was going wrong.
But if I alter the code like :

Here again the compiler does not complain, and on execution, an infnite loop is created. Why is this so? and what does the author mean by "indirectly" in

A compile-time error is generated if a constructor attempts to invoke itself either directly or indirectly

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say you wrote a class something like the following:

When executed, main() will create a new ThisTest by calling the no-arg constructor, which calls the int-arg constructor, which tries to call the no-arg constructor. That is a way of indirectly trying to make a recursive constructor call.
The compiler detects the attempted recursion and (rightly) complains:

Hope this helps!
Regards,
Jeff
 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder if this output is dependent upon the JVM that we use.
Ur example is the same as the one I cited, except for the class names, and I donot get any compile time exception. At run time I get things like StackOverflow! :roll:
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic