Method in java

I am involved in creating custom classes and cannot figure out where I made a mistake.

From the 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 is MyPoint (317, 10) not served in the appropriate class along with x and y?

Any help would be appreciated, thanks.

+4
source share
4 answers

remove return type from

public void MyPoint(int x, int y)
Constructor

cannot have a return type not even void

do it

public MyPoint(int x, int y)
{
    this.x = x;
    this.y = y;
}
+5
source

Constructors do not have a return type. This is the usual method you just did.

Decision. Remove voidfrom the method. It should look like

public MyPoint(int x, int y)
{
    this.x = x;
    this.y = y;
}
+5
source

MyPoint.

public MyPoint(int x, int y)
{
    this.x = x;
    this.y = y;
}
+2

.

public MyPoint(int x, int y)
{
    this.x = x;
    this.y = y;
}
+2

Source: https://habr.com/ru/post/1548299/


All Articles