Skip to main content

Posts

Showing posts from November, 2014

How do I call one constructor from another in Java?

Believe it or not, I don't know this before.
find it from stackoverflow as below,
http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java

Yes, it is possible: publicclassFoo{privateint x;publicFoo(){this(1);}publicFoo(int x){this.x = x;}} To chain to a particular superclass constructor instead of one in the same class, use super instead of this. Note that you can only chain to one constructor, and it has to be the first statement in your constructor body.