5

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());

   }
}
2
  • 5
    You're passing 3 values. You declared your constructor to take only two. How could this work? Commented Mar 10, 2013 at 20:12
  • Java is not java script. Every time you messing them god kills one kitten. Commented Mar 10, 2013 at 20:20

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to change the constructor when you can have another :-) kind sir
0

public PassTheClass (String xMyName, String xMyEmotion) your constructor expects 2 arguments, you pass 3 arguments here:

PassTheClass demo = new PassTheClass("Squidward",94,"proud");

And the compiler says "bleh?"

You probably need another constructor that looks like this:

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

Comments

0

You are calling a constructor with three arguments, but you don't declare one like that. To make the code work you can add a constructor like this:

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

or alternatively:

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

or for a third alternative, keep the first constructor above and replace the 2-argument constructor you have with this:

public PassTheClass(String myName, String myEmotion) {
    this(myName, 0, myEmotion);
}

The third way allows you to have one core constructor that initializes everything, and various convenience constructors that accept defaults for some things and which pass everything through to the core constructor. That minimizes the amount of copying and pasting going on because the assignments happen in only one place.

this differentiates the instance variable from the constructor argument so you don't need to resort to variations on names. Using this is the standard convention the vast majority of Java code uses, following conventions makes your code more readable.

Comments

Your Answer

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