Results 1 to 7 of 7
  1. #1
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default Trying to understand accessor and mutator methods

    Hi,

    I'm trying to understand what mutator and accessor methods do. I've attached some code to this post which is supposed to be an example of these two methods, but even after messing around with it for a while, I still don't get the point of using those methods. Here's the example...

    Java Code:
    public class Marker {
    	//define instance variables, must be defined as private
    	private String color;
    	private double radius;
    	private double length;
    	
    	//mutator methods-change methods and attributes of the object
    	//must be public
    	public void setColor(String c){
    		color=c;
    	}
    	public void setRadius(double r){
    		radius=r;
    	}
    	public void setLength(double l){
    		length=l;
    	}
    		
    	//accessor methods- must return a value
    	//must be public
    	public String getColor(){
    		return color;
    	}
    	public double getLength(){
    		return length;
    	}
    	public double getRadius(){
    		return radius;
    	}
    
    	//instances methods-performs action, changes behavior of the object
    	public void write(){
    	
    	} 
    
    	//Main method goes here
    	public static void main(String args[]){
    		//create object
    		String s="blue";
    		Marker objMarker=new Marker();  // default constructor
    		objMarker.setColor(s);
    		objMarker.setRadius(2.5);
    		objMarker.setLength(15.0);		
    		
    		System.out.println(objMarker.getColor());
    		System.out.println(objMarker.getRadius());
    		System.out.println(objMarker.getLength());
    	}
    
    }
    Now the thing is that when you run this little program, all it displays is "blue, 2.5, 15.0" from this part of the code...

    Java Code:
    		String s="blue";
    		Marker objMarker=new Marker();  // default constructor
    		objMarker.setColor(s);
    		objMarker.setRadius(2.5);
    		objMarker.setLength(15.0);
    Now you see why a newbie like me is having understanding this example. If that's all the program is required to do, we could just write a 5 line program where the user inputs "blue, 2.5, 15.0" and the program displays that! I just don't get it...:confused:

    If anyone has a couple of minutes, I'd really appreciate it if you could go over this and explain it to me thoroughly. Thanks in advance.
    Last edited by DC200; 12-02-2008 at 11:23 PM.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    These become essential for more complex programs. Say this is the default marker your using. You create some code that allows to user to select a new marker. It saves memory by just changing the current marker values to the new user selected ones.

    Or say you have a Person class with attributes like age, name, birth date, city, etc. Say the person moves or changes their name or something. Instead of creating a new Person object you can just do person.setName(newName) person.setCity(newCity).
    That way if this object had any interaction with another class or object or anything you wouldn't need to update anything. It's still the same object just with some values edited.

    Hope that clears it up

  3. #3
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Now I understand what they're used for, but I still don't know how to go about creating one. Your post does clear things up a bit, but it'd be a lot easier for me to understand if someone could explain to me how that relates to each part of that example I posted earlier. Maybe someone could explain to me what each part does or something...

    Also, how we would go about changing the default marker in that example?
    Last edited by DC200; 12-02-2008 at 11:36 PM.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    objMarker is the object your working with. When you initially create it all values are null(or empty). setColor(), setRadius(), setLength() change the null value to Blue, 2.5, 15.0

    Your get methods just return the values you used in the set methods.

    Try changing your code to this to see how it works
    Java Code:
    String s="blue";
    Marker objMarker=new Marker();
    
    System.out.println(objMarker.getColor());
    System.out.println(objMarker.getRadius());
    System.out.println(objMarker.getLength());
                  
    objMarker.setColor(s);
    objMarker.setRadius(2.5);
    objMarker.setLength(15.0);		
    		
    System.out.println(objMarker.getColor());
    System.out.println(objMarker.getRadius());
    System.out.println(objMarker.getLength());
    That will show you the values before and after you set them

  5. #5
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Awesome. :)

    That's all I need to know for now, but just out of curiosity, how would we go about creating a piece of code that changes those three attributes if I remove those three objMarker.set lines at the bottom?

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    you don't. Since you have private attributes the only way to change them is with the set methods. If you changed them to public you could get rid of the set methods and just do (Object.attribute = whatever)
    objMarker.radius = 3.0

  7. #7
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    OK. Thanks for the help. I'll be back another day with another problem. :P

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-02-2008, 12:55 AM
  3. New: Want to understand Drawing...
    By diRisig in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 09:13 AM
  4. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 11:03 AM
  5. mutator method
    By dirtycash in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 11:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •