Difference between Private and Public Variable
A private variable can be seen only by members (properties and methods) inside the class definition. It cannot be seen by code outside of the class definition. On the other hand, a public variable can be seen by code outside the class definition. Code outside the class can just change the public variable of a class by assigning a new value using the class name if the method is static or using the object name if the method is not static. With a public variable, the sense of mutation is not really there. But when a class changes its own value, which cannot be changed from outside, that is called transformation – which has a better sense of mutation.
Mutator Illustration
Not every class needs a mutator. However, when a mutator is needed for a private variable (property), the mutator method which is a member of the class, should begin with “set”. The programmer then defines the mutator method.
Any object in a shop can be defined by a class. A good cup in a shop for example, can be defined by a class. As a minimum, the class would have a property that is the price of the cup, a mutator method and an accessor method. The accessor method is a method to read the value of the property. By convention, the accessor method should begin with “get”. In this case, it would be getPrice(). Let the property with the name price be private. If price is public, then there will be no need for mutator and accessor, as the value could be set or got (read) publicly.
The following class definition is for a cup (an additional property (field) for currency is added):
private double price = 2.0;
private Character currency = '$';
public void setPrice(Double dbl) {
price = dbl;
}
public double getPrice() {
return price;
}
}
The mutator setPrice() is public so that it can be accessed by code outside the class. Making the mutator public does not mean making the corresponding property public. The corresponding property has to be private. The mutator here has the argument dbl which is the new price. As the market conditions change, the price in the shop also changes. If price was public, there would be no need for the mutator setPrice() as it would be possible for code outside the class to change the price. Since setPrice is a member of the class, then it can see the value of the private property price. However, code outside the class cannot see the property. This is by design.
The accsessor getPrice() is public so that it can be accessed by code outside the class. It has no argument. If price was public, there would be no need for the accessor getPrice() as it would be possible for code outside the class to read the price. Since getPrice is a member of the class, then it can see the value of the private property price. However, code outside the class cannot see the property. This is by design.
A mutator setCurrency() and an accessor getCurrency() can similarly be written for the private variable currency.
The following main class and main method accesses the private variable price, mutates the variable, and then accesses the variable again; all these after instantiating the class:
public static void main(String[] args) {
Cup cup1 = new Cup();
double firstPrice = cup1.getPrice();
System.out.print(firstPrice); System.out.print(", ");
cup1.setPrice(3.0);
double secondPrice = cup1.getPrice();
System.out.print(secondPrice);
System.out.println();
}
}
The output is:
The first code segment in the main method instantiates the Cup object (cup1) and accesses the private property price through the accessor getPrice() and cup1. It then prints out this first price with a comma and space.
The second code segment is a one line code segment. It mutates the private property price through the mutator setPrice() and cup1. The third code segment reads and prints the new price.
Validation
The user of the program is supposed to set the new price as a double. But what if he inputs some other data type? The new price should be validated if it is truly a double. The line code for this case should appear like this:
The operator, instance of returns true, if its left operand is an instance of the right operand; false otherwise.
With validation, the mutator method definition should be:
if (dbl instanceof Double)
price = dbl;
else
System.out.println("New Price could not be set!");
}
That is, if the input dbl is of type Double, then the price is changed. If it is not, an error message is issued, and the program continues. Note that ‘d’ for double in this code segment is ‘D’ and not ‘d’.
A similar code to check if the currency is a character can be written as follows:
if (ch instanceof Character)
currency = '€';
else
System.out.println("New currency is not a character!");
}
That is, if the input ch is of type Character, then the currency is changed from $ to ‘€’. If it is not, an error message is issued, and the program continues. Note that ‘c’ for Character in this code segment is ‘C’ and not ‘c’.
Conclusion
There is no predefined method as mutator in Java. A mutator is coded by the programmer. A mutator is just a coded (public) method that changes the private value of a field (property) of a class. On the other hand, an accessor is just a coded (public) method that reads out the private value of a field (property) of a class.
Thank you for reading through our article. Leave a comment about your thoughts and read through other articles on LinuxHint.com that best fit the ideas that you’re looking for.