Mutator Methods

Discussion in 'Mac Programming' started by Dr. No, Dec 9, 2005.

  1. macrumors regular

    Joined:
    Sep 13, 2003
    #1
    Can someone show me the way to set up a Mutator method in Java :confused:
     
  2. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    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;
    	}
    }
    
     
  3. thread starter macrumors regular

    Joined:
    Sep 13, 2003
  4. macrumors 6502a

    NewbieNerd

    Joined:
    Sep 22, 2005
    Location:
    Chicago, IL
    #4
    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:
     
  5. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #5
    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.
     
  6. macrumors 6502a

    NewbieNerd

    Joined:
    Sep 22, 2005
    Location:
    Chicago, IL
    #6
    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.
     
  7. macrumors regular

    Joined:
    Oct 13, 2004
    #7
    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.
     

Share This Page