开发者

Accessor and Mutator Methods confusion

OK开发者_StackOverflow, basically the question was

Accessor and Mutator Methods Suppose that the class Pet has a variable/field called name that is of type String>

Write an accessor method getName() that returns the value of name Enter your answer in this box

and I wrote the code as following:

public getName(String name)
{
    return name;
}

Write a mutator method setName(), with a return type of void that may be used to change the value of name when the pet changes owners. Enter your answer in this box

and i wrote as following:

public void setName(int newName)
{
    name = newName;
}

I seem to be confused as I didnt get the mark, the error I got was

 Pet.java:9: invalid method
 declaration; return type required
 public getName(String name)
        ^ 1 error The output should have been:
     getName() works as expected
     setName() works

This is what was actually produced:
    Exception in thread "main" java.lang.NoClassDefFoundError: Pet

any ideas what wrong with it?


Carefully read the error message: "return type required public getName(String name)".


Your method signatures are both incorrect:

  • name is String as you mentioned, but your mutator method takes an int which means integer as its input. Switch it to String and it should work.
  • Your accessor doesn't actually return the value of the internal name but instead the name parameter given to it. This is an error because accessors are meant to return (a copy of) the value held internally by the class. It's also missing the return type which should of course match the return type of the field.

Oh and just to clarify, method signature means the whole declaration of the method, basically everything you write to declare a method:

  • Name is the obvious one.
  • Return value is part of method signature, really important since it's mandatory.
  • Visibility (public/package/protected/private) is important too because it defines who can access the actual method.
  • Parameters are of course part of the method signature because even if other parts of the method would be entirely same all the way to the name, parameters can make the method entirely unique. Even zero parameters is counted as part of the signature!

The reason it's called signature is of course that these four together form a unique combination which act as an unique identifier to the method and you need all four to have a fully declared method with unique signature. The whole idea of accessors/mutators is relying on a certain pattern for method signatures which enables everyone to access them in a predefines, consice manner.

NoClassDefFoundError usually means something is wrong with classpath, you need to actually add your class to the classpath to be able to run it.


Your accessor should be: public String getName() { return name; }

Your mutator should be: public void setName(String newName) { name = newName; }

and you data should be protected like this

private String name;


My suggestion would be regarding to all comments, to use some IDE for development in JAVA, for example Eclipse, IDE will definitively improve your development time, and prevent pit falls on some basic thinks as this.


Read the error message: return type required public getName(String name) It means that you need a return type for the method. You need to add String after the void so you will have:

public String getName(String name)
{
    return name;
}

You also need to change the int in your setName method to String.

And you need to make sure you are creating the variable name at the beginning of the code:

private String name;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜