i'm learning to create custom classes and can't figure out where I've gone wrong
From main class...
MyPoint p1 = new MyPoint(317, 10);
the error says:
constructor MyPoint in class MyPoint cannot be applied to given types;
required: no arguments
found: int, int
reason: actual and formal argument lists differ in length
this is from my MyPoint class:
private int x, y;
public void MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
Why isn't MyPoint(317, 10) being fed into the relevant class along with the x and y values?
Any help would be appreciated, thank you.
-
3Since when the constructor has a return value?nIcE cOw– nIcE cOw2014年07月14日 05:02:06 +00:00Commented Jul 14, 2014 at 5:02
-
What do you specifically want to do?Jonathan Solorzano– Jonathan Solorzano2014年07月14日 05:14:32 +00:00Commented Jul 14, 2014 at 5:14
-
Give the Definition Constructor is what is called when initialization u have defined it as method. i guess all will be right is following answer.Kishan Bheemajiyani– Kishan Bheemajiyani2014年07月14日 05:18:08 +00:00Commented Jul 14, 2014 at 5:18
4 Answers 4
Constructors don't have return type. This is just a normal method you just made.
Solution: Remove the void from the method. It should look like
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
Comments
remove return type from
public void MyPoint(int x, int y)
constructor cannot have return type not even void
make it
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
1 Comment
You need to declare parameterized constructor in MyPoint class.
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
Comments
Constructors must not have return type. So change your code as
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}