Reputation: 605
In this situation, I have a class with methods that get called to perform certain actions, but I want to make sure a certain condition is true before proceeding. e.g.
public class ActionClass {
public static void checkCondition() {
if(!condition) throw new RuntimeException();
}
public static void performAction() {
checkCondition();
...
}
}
I realize I could make all these methods non-static and use a constructor as a place to perform this condition check and throw an exception there if needed, but I'm curious if there are any other ways to do the same thing?
Upvotes: 0
Views: 43
Reputation: 524
If you want to check a condition every time a method is run, then this is really the only practical way to do it.
Even if you made all the methods non-static, the constructor would only be called once, and the methods would be free to run without the condition check if the check was satisfied at the instantiation of the object.
Upvotes: 1