I have a problem which isn\'t really that big, but still gives me some thought as to how Java constructors and methods are used.
I have a constant representing a rad
You cannot assign to final variable outside constructor. As you said, method:
setRadiuswithCriteria(criteria crit) {
if(... crit ...) {
RADIUS = n;
}
Can be used outside constructor.
And you must set final variable to some value in constructor, not just after checking some criteria (always, not sometimes).
However, you might move the code outside the constructor, using the returned value of some function. Example:
class MyClass {
private final double i;
public MyClass() {
i = someCalculation();
}
private double someCalculation() {
return Math.random();
}
}
How about (using small caps for radius, because it is not a constant, as pointed out in the comments):
public MyProblematicClass(... variables ...) {
radius = getRadiusWithCriteria(criteria);
}
private int getRadiusWithCriteria(criteria crit) {
if(... crit ...) {
return n;
} else {
return 0;
}
}
How about doing like this?
public MyProblematicClass {
public final int RADIUS;
public MyProblematicClass(... variables ...) {
RADIUS = this.setRadiuswithCriteria(criteria);
}
private int setRadiuswithCriteria(criteria crit) {
if(... crit ...) {
return n;
}
return 0;
}