PDA

View Full Version : Mutator Methods




Dr. No
Dec 9, 2005, 11:19 AM
Can someone show me the way to set up a Mutator method in Java :confused:



jeremy.king
Dec 9, 2005, 11:27 AM
Mutator is just a fancy pants term for a setter.

So if your class has an instance variable you would create an appropriate getter and setter method - its that whole encapsulation madness at play here. Convention is a mixed case method name with get and set used as the prefix on the method name.


public class MutatorExample {

private String myField;


/**
* Accessor/Getter
*/
public String getMyField() {
return myField;
}

/**
* Mutator/Setter
*/
public void setMyField(String myField) {
this.myField = myField;
}
}

Dr. No
Dec 9, 2005, 12:26 PM
Thanks! :)

NewbieNerd
Dec 9, 2005, 12:47 PM
I haven't looked at Java 5.0 at all....I know they added generics to match C#, but not the get and set keywords? In C# you can do


public class Person
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
}


and you can just say, if p is a Person,


string s = p.Name;
p.Name = s;


Java still not up to C# level yet? :rolleyes:

jeremy.king
Dec 9, 2005, 12:55 PM
public class Person
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
}



Exactly why is this better :confused: ? So you can reference member fields by a pseudonym rather than method calls, so what. Heck might as well just make your member public and access it directly. I fail to see your beef.

NewbieNerd
Dec 9, 2005, 01:20 PM
Exactly why is this better :confused: ? So you can reference member fields by a pseudonym rather than method calls, so what. Heck might as well just make your member public and access it directly. I fail to see your beef.

Well that was just a simple example. The point is that I can still protect the internals of my class yet have the simplicity of p.Name rather than p.getName. If the field at hand can be mutated, maybe you want to send a clone of it rather than the field itself, etc. You can put any code you want in the get/set blocks, just as in getName and setName, but still access the field in a clean way that looks like you just made the field public.

Another example: suppose a class had counter variable count that needed to be incremented. I could define a method called incrementCount, or I could do this:


p.Count++;


In Java the equivalent would be


p.setCount(p.getCount()+1);


Also note that I don't have to use BOTH get and set. Maybe I wanted read only access to the name. So I could still be able to read p.Name, just not set to it, which you couldn't do by just making the field public to get this simplicity.

A minor point, I agree, but any efforts to keep code neat and simple is worthwhile.

cemorris
Dec 9, 2005, 02:34 PM
Just wanted to throw out my 2 cents here. It is better to return a copy of your variable instead of the actual variable. If you return your private variable the caller method can now modify the classes private variable.