• 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

default constructor?????

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

That does makes sense and nobody denied that. Infact we are were saying the same thing albeit using some different words.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya, I think we were arguing the same point.

At least anybody reading this thread should get a solid understanding of the way constructors work.

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far I know, the rule is very simple for a default constructor insertion by java compiler - if a class does not have ANY constructor(s), ONLY then a default constructor is inserted. The constructor will be a no arg constructor with no/empty body!

 
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
Let's see if this helps...

The compiler will insert a default no-args constructor into a class if and only if no other constructors are provided by the programmer.

The compiler will insert a call to super() as the first line of a constructor if and only if there is no explicit call to "this" or "super." (The exception is the special case of Object, which has no superclass.)

In other words, the first statement in every constructor (with the exception of Object itself) is either an explicit call to "this," an explicit call to "super," or -- in the absence of either of these -- an implicit call to "super."

Any additional code in a constructor executes after that first line returns. Thus, constructors are called from the subclass upwards, but any additional code in the bodies (i.e., anything following "this" or "super") is executed from the base class downwards.

You can call as many constructors as you wish using "this," but eventually you'll need to reach a constructor that calls super (either implicitly or explicitly).

Call order in the above code:
  • "new SomeClass()" calls the no-args constructor, SomeClass().
  • SomeClass() calls SomeClass(String).
  • SomeClass(String) calls SomeClass(int).
  • SomeClass(int) calls SomeClass(boolean).
  • SomeClass(boolean) implicitly calls super(), which is SuperClass(). (Note that the interface implemented by SomeClass is irrelevant.)
  • SuperClass() calls SuperClass(String).
  • SuperClass(String) implicitly calls super(), which is Object().

  • Execution order in the above code:
  • Object() executes.
  • Additional code in SuperClass(String) executes.
  • Additional code in SuperClass() executes.
  • Additional code in SomeClass(boolean) executes.
  • Additional code in SomeClass(int) executes.
  • Additional code in SomeClass(String) executes.
  • Additional code in SomeClass() executes.
  •  
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic