Bango
Bango

Reputation: 288

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?

Upvotes: 1

Views: 1333

Answers (7)

Naruto
Naruto

Reputation: 1

You can simply use the keyword this to call the first constructor (i.e. using a constructor to call another constructor).

Upvotes: 0

nj-ath
nj-ath

Reputation: 3146

You don't need to know, you just need to initialize.

The compiler does what is known as Static binding and matches your call with the signature(the type and number of arguments) of a function(constructor in your case).

Also take a look at this http://javarevisited.blogspot.in/2012/03/what-is-static-and-dynamic-binding-in.html

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

The code below

Shape ball = new Shape(); // takes no parameter

instantiates a new object with a default state. This no-argument constructor is also known as the default constructor since it initializes the object to its defaults.

When invoking a parametrized constructor

Shape ball = new Shape(1,2); // takes parameters

you're instantiating a new object as well as giving it a custom initial state; one that differs from what the above no-argument constructor would have initialized the object with.

Having multiple constructors taking different parameters is known as constructor overloading. Deciding which constructor to use comes down to your requirements. For example, if you had the following two constructors for a class, say, Circle

public Circle() {
    this.center = new Point(0, 0);
    this.radius = 1;
}

public Circle(int x, int y, int r) {
    this.center = new Point(x, y);
    this.radius = r;
}

You would use the parametrized constructor any time you want a circle with its centre different than (0,0) or having a different radius.

Upvotes: 1

Bagzli
Bagzli

Reputation: 6579

When creating an object, you can have a default constructor with no parameters and it would do certain action, so when you provide no parameters this constructor is called. You can then create a second constructor which takes parameters which will then do different action. The term is called Overloading Constructors I believe. Which one gets activated is based on what is provided in the arguments when creating the object.

As per your direct question, to find out which parameters that object takes you have to read the documentation for that object. No other way of finding out. Some tools such as Visual Studio will show you all the available options. However, visual studio is for languages such as c#, not java.

Here is an example you can learn from: http://www.leepoint.net/JavaBasics/oop/oop-45-constructor-overloading.html

Upvotes: 2

Jojo John
Jojo John

Reputation: 400

Programmer should know what are the parameters he need to send through constructor. Whenever he having parameters to send he will pass that values, else he will not pass anything. Thats actually the logic set by the developer who developed the system.

suppose we don't know regarding some class constructors then we can depend the documentation of that particular class.

Upvotes: 0

Kon
Kon

Reputation: 10810

In Java, any method can be overloaded, including a constructor. Basically that means that you can have multiple methods with the same name, but they MUST differ by the type and/or size of arguments they take. So, with your example:

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

Implies that there are at least two constructors to create a new Shape defined by the Shape class somewhere. One takes no arguments, and one takes two integer arguments. You don't have to worry about "choosing" the correct method to call, in a way. What I mean is, Java will choose AUTOMATICALLY which method it runs based only on the parameters you call it with.

Your decision comes in at deciding which parameters you want to pass to the constructor to get your job done the fastest.

Upvotes: 1

Aneesh Ashutosh
Aneesh Ashutosh

Reputation: 772

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
}

Upvotes: 3

Related Questions