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

  • Java
  • Thread starter prosteve037
  • Start date
  • Tags
    Java Type
In summary: 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: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
  • #1
prosteve037
110
3
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:
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??
 
Technology news on Phys.org
  • #2
prosteve037 said:
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:
void
return type specification and still get have it return/get/give values?

A void method, by definition, cannot return a value, and this means that you can't have a statement such as return x; in the body of such a method.

The purpose of accessor and mutator methods is to read and set, respectively, values of data members of a class. These methods are important if the data members are private, which is the way things are usually done. For instance, if a class has a private member named length, a programmer would normally implement a function to get the value of length (an accessor method) and one to set its value (a mutator). The accessor method (the "getter") would have to return a value of the same type as length. The mutator (the "setter") wouldn't return a value, so would be a void method.
Unless, I'm looking at this the wrong way and the code is supposed to return some other part of code??
Code can't return "some other part of code."

Hope that helps.
 
  • #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:
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.
 
  • #4
Adyssa said:
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:
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;
    }

    [B]int[/B] getColor()
    {
        return color;
    }

    void setColor(Color color)
    {
        this.color = color;
    }
}

Shouldn't it be Color getColor()? And shouldn't the instance variables have underscores before their names?

A mutator method returns nothing (void) but takes an input parameter (Color color). The accessor methods return (int) and (Color) but take no parameters.

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?
 
  • #5
prosteve037 said:
Shouldn't it be Color getColor()?
Yes. I suspect Adyssa was just providing an example, but didn't check it very closely.
prosteve037 said:
And shouldn't the instance variables have underscores before their names?
That's a matter of style, and not a matter of syntax.
prosteve037 said:
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?
Of course. Don't read too much into the example. It's more of a teaching tool than anything else.
 
  • #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:
Color getColor(int something, double something_else, char another, int yet_another)
{
    // do something with all those variables!
    return color;
}
 
  • #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?
 
  • #8
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.
 
  • #9
Here's Adyssa's example again, with much of it stripped out.
Code:
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:
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:
 return id;
 

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

What are accessor and mutator methods in Java?

Accessor methods, also known as "getters," are Java methods that are used to retrieve the value of a private field in a class. Mutator methods, or "setters," are used to modify the value of a private field.

Why do we need accessor and mutator methods in Java?

Accessor and mutator methods are used to ensure proper encapsulation in Java. By making fields private and providing methods to access and modify them, we can control how the data is accessed and changed in our program.

When should I use a return type specification in my accessor or mutator methods?

You should use a return type specification in your accessor methods if you want to return a value to the caller. Mutator methods, on the other hand, do not typically require a return type specification since they are used to modify the value of a field and do not need to return anything.

Can I have an accessor or mutator method without a return type specification?

Yes, you can have an accessor or mutator method without a return type specification. In this case, the method will have a void return type, indicating that it does not return anything.

What happens if I try to access or modify a private field without using an accessor or mutator method?

If you try to access or modify a private field without using an accessor or mutator method, you will receive a compile-time error. This is because private fields are only accessible within the same class, so you need to use accessor and mutator methods to properly access and modify their values.

Similar threads

  • Programming and Computer Science
Replies
2
Views
714
  • Programming and Computer Science
Replies
2
Views
715
  • Programming and Computer Science
Replies
1
Views
802
  • Programming and Computer Science
Replies
3
Views
3K
Replies
63
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top