Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Dr. No

macrumors regular
Original poster
Sep 13, 2003
193
0
Can someone show me the way to set up a Mutator method in Java :confused:
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
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.

Code:
public class MutatorExample {

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

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

NewbieNerd

macrumors 6502a
Sep 22, 2005
512
0
Chicago, IL
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

Code:
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,

Code:
string s = p.Name;
p.Name = s;

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

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
NewbieNerd said:
Code:
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

macrumors 6502a
Sep 22, 2005
512
0
Chicago, IL
kingjr3 said:
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:

Code:
p.Count++;

In Java the equivalent would be

Code:
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

macrumors regular
Oct 13, 2004
138
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.