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

  • Context: Java 
  • Thread starter Thread starter prosteve037
  • Start date Start date
  • Tags Tags
    Java Type
Click For Summary

Discussion Overview

The discussion revolves around accessor and mutator methods in Java, focusing on their purpose, implementation, and the necessity of return type specifications. Participants explore theoretical and practical aspects of these methods, including their roles in encapsulation and data manipulation within classes.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants express confusion about the necessity of having both accessor and mutator methods, questioning if a void method could suffice for returning values.
  • Others clarify that accessor methods are designed to return values of private data members, while mutator methods are intended to set those values, emphasizing the importance of these methods when data members are private.
  • A participant provides an example class, detailing how accessor and mutator methods function, including the distinction between return types for these methods.
  • Some participants suggest that the accessor method should return a Color type instead of an int, raising questions about coding style and conventions, such as the use of underscores in variable names.
  • There is a discussion about the potential need for additional instance variables and parameters if a car were to have multiple colors, indicating a need for more complex data representation.
  • One participant inquires about the role of the return keyword in accessor methods, seeking clarification on its function within the method definition.
  • Another participant argues that accessor and mutator methods are not fundamentally different from other Java methods, suggesting that their naming may complicate understanding.
  • A later reply illustrates how the return value from an accessor method can be assigned to a variable, reinforcing the practical application of these methods.

Areas of Agreement / Disagreement

Participants generally agree on the basic roles of accessor and mutator methods, but there is ongoing debate regarding their implementation details, coding style, and the necessity of certain features. The discussion remains unresolved on some specific coding practices and the implications of using these methods.

Contextual Notes

Limitations include varying interpretations of coding style, the potential for multiple approaches to representing data, and the unresolved nature of certain technical details regarding method implementation.

prosteve037
Messages
110
Reaction score
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
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.
 
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.
 
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?
 
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.
 
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;
}
 
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?
 
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.
 
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;
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
3
Views
4K
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K