+ 4
So, a time ago I got the problem that a function starts a new thread, and I needed 6 downloads, every one in a single thread.
Later I had to work with the downloaded stuff, but the program should start working with it when ALL downloads were completed.
The problem was solved using a setter method. It incremented a counter and checked the new counter value. When it became six, the program could work with my downloads, every thread called my setter method, so it worked.
With setters you can control what value your variable musn't have for example:
public static int x;
static int divideByX(int divident){
return divident / x;
}
static void setX (int set){
if (set != 0){
x = set;
return;
}
System.out.println ('You must not divide by Zero!");
}
Without my setter, I could assign 0 to x, which would cause a ZeroDivisonError.
+ 2
Do it just in case. You are not using it now but maybe need it in the future.