• 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

this() or super() first line in constructors?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I have a doubt from K&B, Chapter 2- Constructors and Instantiation:
"Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler."

This is not necessarily true.

I a simple class:
class tester{
tester(){
System.out.println("in tester()");
}
tester(int i){
System.out.println("in tester(int i)");
}
public static void main(String[] args){
tester t = new tester(10);
}
}

Output:
in tester(int i)


In this code example, this() is not invoked from tester(int i) constructor.
Am i missing something here? I am giving SCJP in 9 days time and i guess i have lost some logical reasoning to tension!

Please help.
Thanks,
Meher
 
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

Originally posted by Meher Parveen:
...In this code example, this() is not invoked from tester(int i) constructor...


That's correct. But super() is implicitly invoked as the first line of that constructor (before the println executes).
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler."

If a constructor has neither a this() nor a super() call as its first statement, then a super() call to the default constructor in the superclass is inserted.

if used, the super()/this() call must occur as the first statement in a constructor. This implies that this() and super() calls cannot both occur in the same constructor.
 
cm lak
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler."

If a constructor has neither a this() nor a super() call as its first statement, then a super() call to the default constructor in the superclass is inserted while compiling.
if used, the super()/this() call must occur as the first statement in a constructor. This implies that this() and super() calls cannot both occur in the same constructor.
 
Meher Parveen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the response.... I understand the super() to Object class is called implicitly hence the this() constructor is not called.

Meher
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic