I have a class with has some miniclasses within it, which act as “storage units” for constants. The problem is, I need some information to assign values to these constants, and I only get that information in the constructor. Unfortunately, the miniclass is not declared in the constructor, and NetBeans tells me that I can’t assign to a final variable, even if it’s only been declared, not initialized. How should I go about this?
Edit: An example of some code that would have this problem:
public class Car {
class constants {
public final String MAKE;
public final String REGISTRATION_NUMBER;
}
public Car(String make, String regNumber) {
constants.MAKE = make;
constants.REGISTRATION_NUMBER = regNumber;
}
}
What you are trying to do can’t work as you need an instance of your
Constantsclass somewhere. One way would be to do as below, but then there is nothing constant about that constant…You can then call:
Note: a probably better (and simpler) design would be