Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java - Accessor/Mutator Methods and when you need a return type specification

  1. Oct 19, 2011 #1
    We learned of accessor and mutator methods in class recently. But even after a few labs implementing the two types, I still am having trouble understanding when to use/implement either one.

    I don't understand why you'd need two different method types. Couldn't you set any method with a
    Code (Text):
    void
    return type specification and still get have it return/get/give values?

    Unless, I'm looking at this the wrong way and the code is supposed to return some other part of code??
     
  2. jcsd
  3. Oct 20, 2011 #2

    Mark44

    Staff: Mentor

    Code can't return "some other part of code."

    Hope that helps.
     
  4. Oct 20, 2011 #3
    The idea is that you have this private variable in your class, and if another part of your program needs to use an object from that class, then it needs a way to a) set the value and b) get the value. You might have something like this:

    Code (Text):
    public class Car
    {
        private:
        Color color;
        int id;

        public Car(Color color, int id) // class constructor function
        {
            this.color = color; // set the color
            this.id = id;         // set the id
        }

        int getID()
        {
            return id;
        }

        int getColor()
        {
            return color;
        }

        void setColor(Color color)
        {
            this.color = color;
        }
    }
    So this class has a constructor function to initialise the object with a color and an ID. Then it has two accessor methods to return these values, in the event some other part of your program needs to know the ID or the color of the Car object. Then we have one mutator method for the color, so you can change the color of the car. There is no mutator method for the ID because you shouldn't change this.

    A mutator method returns nothing (void) but takes an input parameter (Color color). The accessor methods return (int) and (Color) but take no parameters.
     
  5. Oct 20, 2011 #4
    Shouldn't it be Color getColor()? And shouldn't the instance variables have underscores before their names?

    If the car was defined to have more than one colour as part of its representation, would you need both a new instance variable to represent the car's second colour and also a parameter for it in the getColour method?
     
  6. Oct 21, 2011 #5

    Mark44

    Staff: Mentor

    Yes. I suspect Adyssa was just providing an example, but didn't check it very closely.
    That's a matter of style, and not a matter of syntax.
    Of course. Don't read too much into the example. It's more of a teaching tool than anything else.
     
  7. Oct 21, 2011 #6
    Yeah sorry the getColor() function should return a Color type not an int, I didn't see that error. With regards to extra colors, or extra parameters in general - sure, you should create variables for all of the information you wish to store about an object, and accessor/mutator functions to interact with them.

    You could have a more complex color variable to hold multiple colors and other color-y information if you wanted. Probably a small class in itself. This way you could just return a CarColor object (or whatever) in your accessor, or you could have a few different accessors for separate color variables, depends how you want to do it.

    You can only return one "thing" from a function, be it a primitive type (int, float, double) or an object (String, Color, etc), but you may pass more than one parameter to a function:

    Code (Text):
    Color getColor(int something, double something_else, char another, int yet_another)
    {
        // do something with all those variables!
        return color;
    }
     
  8. Oct 21, 2011 #7
    I think the real question I'm trying to ask here is, what does the return keyword in the accessor method definition do?

    I've been writing it in my codes for labs at school, but have never seen it in action. What does it do?
     
  9. Oct 21, 2011 #8

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    There is nothing "special" about accessor and mutator methods, except some people like giving things fancy names to make them seem more complicated than they really are. The code works exactly the same was as for any other Java method.

    The expression after the "return" keyword is the value returned by the method.
     
  10. Oct 21, 2011 #9

    Mark44

    Staff: Mentor

    Here's Adyssa's example again, with much of it stripped out.
    Code (Text):

    public class Car
    {
        private:
          Color color;
          int id;

        int getID()
        {
            return id;
        }
    }
     
    Now let's look at how this code would be used. Suppose we have a Car object named aCar that has already been initialized with an ID of 12345 and a color of "red".

    I can get the ID like this:
    Code (Text):

    int car_ID;

    car_ID = aCar.getID();
     
    The value that is stored in car_ID is the same value that is returned in the code for getID(), in this statement:
    Code (Text):
     return id;
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java - Accessor/Mutator Methods and when you need a return type specification
  1. Return type void (Replies: 1)

Loading...