jump to navigation

Constructor June 18, 2007

Posted by Allu in JAVA.

The automatic initialization is performed through the constructor, constructor has same name has class name. Constructor has no return type not even void. We can pass the parameters to the constructor. this() is used to invoke a constructor of the same class. Super() is used to invoke a super class constructor. Constructor is called immediately after the object is created before the new operator completes.

 Constructor can use the access modifiers public, protected, private or have no access modifier

 Constructor can not use the modifiers abstract, static, final, native, synchronized or strictfp

 Constructor can be overloaded, we cannot override.

 You cannot use this() and Super() in the same constructor.


1. Anil - February 19, 2010

another small thing, You can not have both super() and this() invocation in the same constructor. It has to either one of those. And it has to be the first statement in the constructor.
i.e if u have class Class1
then Class1(){this();super();} or Class1(){super(); this();} is illegal
you can have either
Class1(){this();} or Class1(){super();}

2. Mayank goyal - April 11, 2013

and in the case of constructor the first statement should be super type or this type

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: