Why are mutator and accessor methods necessary

What are accessor methods?

The secret principle to be used in object-oriented programming and the data encapsulation require that variables and methods of a class can only be accessed under controlled conditions. For this reason, instance variables in particular are often declared so that they are only visible in the declaring class itself. In this way, it can be ensured, for example, that the internal handling of the variable values ​​itself can remain completely invisible from the outside, so that the respective implementations can be varied as required without having to take account of the access.
Furthermore, cases are conceivable in which a validity check should first be carried out before a value is assigned to a variable in order to avoid setting impermissible or invalid values, for example. Read access to variables must also remain controllable, e.g. to ensure the expected formatting.

Of course, getter and setter methods only have to be implemented if they are actually used. Rather, dispensing with a setter method can even protect a variable from unqualified access.

The identifiers of accessor methods are made up of one for reading and one for writing methods, followed by the variable identifier written with an initial capital letter. This convention should be strictly adhered to, as this provides standardized interfaces to the outside world, which are known from the outset without extensive familiarization with the source text.

import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; public class arithmetic {public static void main (String [] args) {divisor divisor = new divisor (); divider.setDividend (26); Teiler.setDivisor (3); divider.part (); System.out.println (parter.get result ()); }} class divider {private double dividend = 0, divisor = 1, result; public void parts () {result = dividend / divisor; } public void setDividend (double d) {dividend = d; } public void setDivisor (double d) {divisor = 1; if (d! = 0) {divisor = d; }} public double getErresult () {NumberFormat format = NumberFormat.getNumberInstance (Locale.US); ((DecimalFormat) format) .applyPattern ("#. ##"); String dStr = format.format (new Double (result)); return new Double (dStr) .doubleValue (); }}

The example1 demonstrates the use of getter and setter methods using a simple division.
In the class, three variables, one of which is a dividend and a divisor, are declared as instance variables and initialized with or. All three variables cannot be accessed directly from outside the class.
However, this is made possible by one getter and two setter methods, which, thanks to their declaration, can also be called from outside. Before assigning the value transferred as a parameter, a safety check is carried out, which prevents the divisor from accepting the value and thus a mathematically forbidden division by can take place. The getter cuts the result to two decimal places before it is returned.

1) The example does not provide a meaningful arithmetic solution. Security queries, exact roundings, etc. were omitted for reasons of clarity in the example code.