Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method issue in java

Tags:

java

methods

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.

like image 844
MalcolmTent Avatar asked Dec 08 '22 07:12

MalcolmTent


2 Answers

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;
}
like image 94
Gaurav Gupta Avatar answered Dec 11 '22 09:12

Gaurav Gupta


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;
}
like image 42
jmj Avatar answered Dec 11 '22 09:12

jmj