Java constructor problem

class, java

Solution

Your constructor accepts `String, String`, but you're passing `String, int, String`.

Either add an `int` parameter to the constructor, or remove the `int` from the call.

My suggestion is to add an int parameter, ie change your constructor to this:

public PassTheClass (String xMyName, int xMyGrade, String xMyEmotion) {
    myName = xMyName;
    myGrade = xMyGrade;
    myEmotion = xMyEmotion;
}

If you still need the `String, String` constructor, change it to call the 3-arg one and pass in the initial value you currently have coded:

 public PassTheClass (String xMyName, String xMyEmotion) {
     this(xMyName, 0, xMyEmotion);
 }

Although there's nothing "wrong" about prefixing parameters with "x" to distinguish them from field names, I have never seen such a thing done. The convention in java is to use the same name for the parameter as the field name and use `this.` when assigning, like this

public PassTheClass (String myName, int myGrade, String myEmotion) {
    this.myName = myName;
    this.myGrade = myGrade;
    this.myEmotion = myEmotion;
}

As a further "style" improvement, don't prefix your field names with "my". Every field is implicitly "my" something; just name them plainly, ie `name`, `grade` and `emotion`.

Good code is all about clarity: Prefer avoiding prefixes, as they just clutter the code and reduce readability.

Problem

I created a class file, and a tester file. When I tried to use a constructor to create an object it wouldn't compile. It said "PassTheClassTester.java:5: error: constructor PassTheClass in class PassTheClass cannot be applied to given types;" Please help. Here is my code: ``` public class PassTheClass { private String myName = " "; private int myGrade; private String myEmotion = " "; public PassTheClass (String xMyName, String xMyEmotion) {myName = xMyName; myGrade = 0; myEmotion = xMyEmotion;} public String getMyName() {return myName;} public int getMyGrade() {return myGrade;} public String getMyEmotion() {return myEmotion;} public void setMyName (String yMyName) {myName = yMyName;} public void setMyGrade (int yMyGrade) {myGrade = yMyGrade;} public void setMyEmotion (String yMyEmotion) {myEmotion = yMyEmotion;} } public class PassTheClassTester { public static void main(String[] args) { PassTheClass demo = new PassTheClass("Squidward",94,"proud"); System.out.println(student.getMyName()); } } ```

Original source