简体   繁体   中英

how to call parameterized constructor from default constructor?

I want to call parameterized constructor from default constructor inside a public java class.

Can I achieve it?

public Rectangle()
{
Rectangle(10.5f,2.5f)     //this not working
}
public Rectangle(float length, float breadth)
{
code...
}

You can use the this keyword.

This should do the trick:

public Rectangle() {
    this(10.5f, 2.5f);
}

public Rectangle(float length, float breadth) {
    //code..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM