Two constructor calls inside one constructor in Java -
suppose following situation:
class foo{ foo(i i, i1 i1){ super(); this(i); ... } foo(i i){ super(); ... } }
java complains constructor call must first statement in constructor. cannot make 2 constructor calls first @ same time. there workaround wouldn't repeating code of one-argument constructor inside 2 argument constructor?
yes, mentioned in comment , don`t need call super(). how works in java ?
if don`t write constructor, java write to you, when compiling code , inserting:
public class apple{ public apple(){ // injected java, super(); // object class }
if write constructor, don`t add java replace yours:
public class apple{ public apple(){ super(); // added java, during compiling }
if write own:
public class apple{ public apple(){ // init things , make world better place }
in case java don`t insert anything, super(); because need call object class well. so, super(); there @ cost.
if have multiple constructor calls, this() , java add super(); // object class call @ top of constructor.
sources: https://docs.oracle.com/javase/tutorial/java/iandi/super.html https://docs.oracle.com/javase/tutorial/java/iandi/objectclass.html , http://www.amazon.co.uk/programmer-study-1z0-803-1z0-804-oracle/dp/0071772006/ref=sr_1_1?s=books&ie=utf8&qid=1435233062&sr=1-1&keywords=ocjp
Comments
Post a Comment