• Post Reply Bookmark Topic Watch Topic
  • New Topic

coreJava  RSS feed

 
Sri In
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Java specs says this() or super() shuld be
the first line in the constructory.Actually I want to know why this() cannot be called after super() .
To be more elaborate if in my constructor I am calling super(3)(a call to super class constructor with argument)
after that call ,why cant I call this() .

Have a look at the example below
class A{
public A(){

}
public A(int a){
super();


}


}

class B extends A{
public B(){

}

public B(int a){
super(3);
this();
}
}

at call to this() giving compilation error.

what might be the possible reason behind that rule.
Can anybody please help me out.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If this was allowed, wouldn't the super class get "constructed" twice? Once with super(3). And once with super(), when the this no-arg constructor calls the super no-args constructor.

Henry
 
Sri In
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry
Thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!