what does this()
mean in Java?
It looks it is only valid when put
this();
in the class variable area.
Any one has idea about this?
Thanks.
It means you are calling the default constructor from another constructor. It has to be the first statement and you cannot use super() if you have. It is fairly rare to see it used.
It's a call to the no-argument constructor, which you can call as the first statement in another constructor to avoid duplicating code.
public class Test {
public Test() {
}
public Test(int i) {
this();
// Do something with i
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With