0

Can we use this() and super() both in the constructor ??

18th Jul 2016, 5:29 PM
AnsHuman Aaditya
AnsHuman Aaditya - avatar
3 Answers
+ 3
No, those keywords have to be first in the constructor. Since both keywords can't be first at the same time, you can't use them together.
18th Jul 2016, 6:12 PM
James
James - avatar
0
could you explain what it means to be "first" in the constructor,please.??
18th Jul 2016, 6:18 PM
AnsHuman Aaditya
AnsHuman Aaditya - avatar
0
When you create the constructor, it has to be the first statement. Example: class MyClass public MyClass(){ this(); System.out.println("other stuffs"); } this() is first, if you put the println before this() it'll give you a compilation error. Because of that, you can't have both super() and this() because only 1 can be first public MyClass(){ System.out.println("won't work"); this(); } will give compile error
18th Jul 2016, 6:25 PM
James
James - avatar