Bango Bango - 1 year ago 46
Java Question

how do you know what parameters go inside the parentheses when creating a new object?

I am in the process of learning java programming.
so sometimes i see code that looks like this:

Shape ball= new Shape();.// takes no parameter
Shape ball= new Shape(1,2); //takes parameter

My question is, when an object has multiple constructors that take different paramaters, how do you know which constructor to use?


Look at the documentation for the class. That's the only surefire way to know. The Shape class you provided likely has two constructors:

Shape() //default no-args
    //do something

Shape(int a, int b)
    //do something with a and b