Reputation: 6740
Can anybody explain the differences between these constructors. See this;
public MyClass() {
}
and
public MyClass() {
this();
}
and
public MyClass() {
super();
}
Upvotes: 1
Views: 849
Reputation: 454
Lets look at the three type of constructor calls one by one.Each one has special purpose associated with them. But the concept is simple and very interesting to explore.
Case 1 :- This is default constructor with no arguments
public class MyClass{
public MyClass(){
}
}
The compiler will automatically supply a no-argument constructor for you if you do not write one.Thus if you write public class MyClass{ }
.
This is equivalent to writing
public class MyClass{
MyClass(){ }
}
Points to be noted :-
Compiler automatically provide "super" when you do not use super "as the first line of the constructor". NOTE:- For "super" and "this" when you use in constructors always write them in the first line of your constructor code otherwise compiler will give error.
Even when you do not extend MyClass here compiler will give call to constructor to super Object class which is the root class of every class you create.
Case 2:- The use of "this".
Constructors use "this" to refer to another constructor in the same class with a different parameter list.
public MyClass() {
this();
}
So the above code will give compilation error "Recursive Constructor Invocation" as it is calling the same constructor and will end up in an infinite loop. To make this work you can instead write
public MyClass(String str){
System.out.println("constructor with arg "+ str);
}
public MyClass(){
this("Cons"); // note that "this" has to be at first line otherwise compiler gives error
System.out.println("constructor with no arg");
}
Here you can see the importance of "this" in constructors. It is used to call constructors from another constructor in the same function.
Case 3:- Use of "Super". Super is used to make a call to no-arg constructor of super call. Rest information is mentioned in Case 1 already.
public MyClass() {
super();
}
Upvotes: 1
Reputation: 1825
It is very simple
Let me make you understand
first
public MyClass() { }
is simply a default public constructor
But when you write this
public MyClass() { this(); }
that means you are applying constructor chaining. Constructor Chaining is simply calling another constructor of the same class. This must be the first statement of the Constructor. But in your scenario you are passing nothing in this();
that means you are again calling the same constructor which will result in infinite loop. your class may have another constructor like this
public MyClass(int a) { }
then you may have called this
public MyClass(){ this(10); }
the above statement will make you to jump to the constructor receiving the same arguments that you have passed
Now,
public Myclass(){ super(); }
signifies that you are calling the constructor of the super class which is inherited by the class MyClass
. the same scenario occurs here, you have passed nothing in the super();
which will call the default constructor of the Super class.
Upvotes: 1
Reputation: 718678
I think that in the second case you probably meant to write:
public MyClass() {
super(); // not "this();"
}
Either way, your second case is a compilation error. The JLS says this:
"It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this." JLS 8.8.7
In your example, this()
is an explicit constructor invocation that directly invokes the same constructor.
Finally, there is no semantic difference between the first and third forms. The first form has an implicit invocation of the no-args constructor of the superclass, and the third for just makes that invocation explicit.
Some people prefer to write the form with an explicit invocation for the sake of readability, but IMO it doesn't really make any difference.
Upvotes: 1
Reputation: 19185
Second is not allowed to be declared in java because you are making recursive call to default constructor only. Also by default constructor has always super() method which is inserted by compiler if not specified explicitly.
Upvotes: 3
Reputation: 2137
default constructor doing nothing special. each class in java has it per default until you write your own constructor, then you have to define it by your own again
public A() {}
public A(int value) {}
is the default constructor with a recursive call on it (invalid and shouldnt be used). with this()
you call default constructor. for example with this(5)
you call a defined constructor which takes an int value as parameter like the example from 1.
public A() { this(5); }
public A(int value) {}
default constructor which calls the default constructor from your super class
abstract class A { public A() {} }
class B extends A { public B() { /* calls A() */ super(); }
Upvotes: 0
Reputation: 33534
this()
will be used to call MyClass()
No-Args Constructor.
Though it will give Recurssion constructor error
during Compilation
Eg:
public MyClass{
public MyClass(){
this(5); // Here this(5) will be used to call the MyClass Constructor with
// int value as Parameter
}
public MyClass(int i){
}
}
Upvotes: 0
Reputation: 51421
The second one will surely cause an OutOfMemoryError exception due to a stack overflow not compile due to a Recursive constructor invocation
error as you're calling the same constructor from itself. Did you mean to write
super();
? In that case both forms are identical.
public MyClass() {
}
Will call the default no-args constructor of the base class implicitly, if it exists.
Upvotes: 0
Reputation: 13779
public MyClass() { }
declares an empty no-arg constructor. If you declare no constructor for a class, the compiler effectively inserts this conrtsuctor for the class.
public MyClass() { this(); }
will not compile.
public MyClass() { super(); }
is a constructor which invokes the default constructor of the super-class. This will compile only if the super-class has a default constructor, or no constructor (in which case, the compiler inserts a default constructor anyways, as mentioned above). Also, leaving out the super();
call has the same effect.
Upvotes: 2
Reputation: 16158
public MyClass() { }
--> this is normal constructor also called default constructor.
public MyClass() {
this();
}
--> You are making constructor recursive. Don't use this way. Well you can make use of this()
like :
public MyClass() {
this(1); // always intitalize to 1
}
public MyClass(int i) {
this.i=i;
}
Upvotes: 0
Reputation: 5946
From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
But second one leads to recursive constructor invocation and not allowed.
Upvotes: 0
Reputation: 2298
The first is the default empty constructor. It simply does nothing. If you don't have other constructors (with arguments), you don't have to define this sort of constructor at all. It is implicitly made default if no constructors are defined.
The second calls itself which causes a recursive loop, hence is forbidden.
The third calls the default no-args constructor of the super class but does nothing more. The super class is the one that your class extends
from, or Object
(implicitly extended if you don't specify the extends
keyword).
Upvotes: 0
Reputation: 5121
In the first case the constructor will insert a silent call to no argument constructor of the super-class which I guess in this case will be the Object class. In the second case the this() call will call the no argument constructor of class. It can sometimes cause an error too.
Upvotes: 0