Accessor and Mutator in Java

1.6k views Asked by At

For instance in main method I havesetFirstName() and call the Contact class to do this. In the contact class I know I should use:public String getName() and then public void setFirstName(String name)

I know this is the right way to do that. But instead, I find that if i do:

public String setFirstName(String name)
{
    return this.name=name;
}

this will also work and I don't need to use getName()method.

Can anyone please help me out why is that? Is my accessor/mutator method correct?

3

There are 3 answers

0
Hulk On BEST ANSWER

It is the very idea of an accessor that it only reads but does not modify. That is a useful convention, because if done consistently it makes reasoning about some code you don't know a lot easier. Normally, you shouldn't have to expect weird side effects from something named getX(). It may be ok to do some lazy initialization, but it is clearer if such "lazy getters" are named getOrCreateX() or similar to give the user a hint that the first use might take a bit longer/involve some expensive operation.

A mutator that returns something may be useful, but it will help users of your class if you name it in a way that gives a hint about what it returns. Is it an error code or the previous value? People don't expect return values from something named setX(), so they will be surprised to see such a signature and have to look at the documentation of your class to see what it does.

Summary: Always code as if someone else will have to take over your code any moment - don't do things that would be surprising. Even if you never hand your code to anyone else, your future self will thank you for picking names that explain what the method does.

Note: Your implementation of the setter is not very useful, as it always returns the new value you just passed in.

0
user253751 On

Getters and setters are just a convention. The compiler doesn't recognize them specially, or anything like that.

A getter, for example public String getFirstName() {return this.name;}, is just a method that returns the value of a field.

A setter, e.g. public void setFirstName(String name) {this.name=name;}, is just a method that sets the value of a field to its argument.

There is nothing forcing you to create getters and setters, and nothing bad happens if you don't, or if you decide to implement them differently. The reason they're normally written that particular way is that it works well most of the time. But you don't have to.

 

You have created a method that:

  • Sets the value of the name field to its argument.
  • Returns the value of the name field.

Nothing wrong with that, it's a perfectly valid method.
Is it useful as a setter? Sure, it sets the value.
Is it useful as a getter? Nope - tell me, how would you use it to get the person's first name?
But it's a perfectly valid method, and it's not "right" or "wrong", it's just not particularly useful.

0
KDP On

The setters and getters must be properly implemented because some frameworks depends on them for proper functioning.

Mocking frameworks such as Mockito ,DI frameworks and some xml serialization techniques depend on set and get methods.