Im a beginner at Java and im told to make getters in a class to get the variables of objects. I forgot to call the getter methods, but this didnt seem to be a problem for the outcome of my code. Below i provided some of the code i made:
public class Money
private final int euros;
private final int cents;
public int euros() {
return this.euros;
}
public int cents() {
return this.cents;
}
public Money minus(Money decreaser) {
int NewEuros = this.euros - decreaser.euros;
int newCents = this.cents - decreaser.cents;
im told the correctway to get the variables euros and cents from the decreaser object is to use the getters like so:
public Money minus(Money decreaser) {
int NewEuros = euros - decreaser.euros();
int newCents = cents - decreaser.cents();
both gave me the results i was looking for. So now i am left confused about the use of getters. Could someone explain me why i should use getters to get the variables of an object?