• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic