• 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

a static method call in a constructor?

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

In the following code, I don't understand why only a static method and not an instance method can be used in Animal() constructor. K&B says

"an instance method cannot be invoked until after the superclass constructor has run."

Can you please explain this statement with respect to this code...

Also please show me the place where a super() is implicitly inserted...



Thanks.
[ June 30, 2007: Message edited by: Vishwa nath.K ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishwa,
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a constructor if you call this() the compiler won't implicity insert super(). In the constructor Animal(), there is a this() called, and for this reason the compiler don't insert super(), whereas in constructor Animal(String name) the compiler implicity adds super() (at the first line).

If you take a look at the order of execution, you'll see that, first of all Animal() is called, then Animal() calls this(makeRandomName()) but we still don't arrived to super() and as k&B says makeRandomName() has to be static because superclass is still not constructed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic