4

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.

nIcE cOw
24.6k8 gold badges54 silver badges147 bronze badges
asked Jul 14, 2014 at 5:00
3
  • 3
    Since when the constructor has a return value? Commented Jul 14, 2014 at 5:02
  • What do you specifically want to do? Commented 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. Commented Jul 14, 2014 at 5:18

4 Answers 4

6

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;
}
answered Jul 14, 2014 at 5:02
Sign up to request clarification or add additional context in comments.

Comments

5

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;
}
answered Jul 14, 2014 at 5:01

1 Comment

ah, completely spaced the return statement - must have confused it with some other work. Thanks
2

You need to declare parameterized constructor in MyPoint class.

public MyPoint(int x, int y)
{
 this.x = x;
 this.y = y;
}
answered Jul 14, 2014 at 5:02

Comments

2

Constructors must not have return type. So change your code as

public MyPoint(int x, int y)
{
 this.x = x;
 this.y = y;
}
answered Jul 14, 2014 at 5:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.