Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Competition between “this”and “super” in constructor

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


error : Constructor call must be the first statement in a constructor
Can any body explain the reason for error ?
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Have a look at this
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be one way to construct an object. If we allow to use it as you gave, how many ways are there to construct an object? Won't there be a ambiguity in those two ways as you gave?
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A super() call, as well as a this() call, calls a constructor and thus has to be the first statement in a constructor. If you put the super call first, the this call is not the first statement, and if you put the this call first, the super call is not the first statement, which will cause the compiler to complain.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic