• 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

Recursive constructor invocation

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

I have the following code:



I am using "javac 1.5.0_06". The compiler spits out the following error:
TestThis.java:9: recursive constructor invocation
public Dummy( String d ){
^
1 error


The excerpt from book "SCJP 5 Study Guide" is "your compiler may not actually catch the problem (it varies depending on your compiler, but most won't catch the problem).

for the following code:



It compiles fine, of course runtime error.

Who shall we listen to for the recursive constructors case during the exam? Select it compiles, or select its compilation fails?

THanks.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there would be one more option in the exam ... depends on the VM
 
warren li
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akash Bhatt:
there would be one more option in the exam ... depends on the VM



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

Compiler catches the recursive constructor call because at the compiler time it checks the way to reach the parentclass(in our case Object class).

Compiler automatically adds super in the first line of the every Constructor (provided we aren't given and this() is not in the first line of the constructor)

So in our case it cannot add super in both of the constructor because this() is present in the first line. so it throws error.

But in case of method,
Compiler just checks for syntax and other stuff(not a flow).

Correct me if i am wrong
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic