A wrapper class for storing primitive parameters which are initialized with values. Is it better to use public static
or private static
with bunch of getters and setters, for these constants?
public class Wrapper {
// This
private static int PARAMETER = 100;
/**
* EDIT:
* public int getParameter();
* public void setParameter(int n);
* Pointed out by @JimmyJames that I forgot the static accessor.
*/
public static int getParameter();
public static void setParameter();
// Or this?
public static int ANOTHER_PARAMETER = 200;
}
My observations:
1. These parameter are rarely changed.
2. I feel like it reduces readability when there are a lot of these parameters, if using private static
with lots of getters and setters.
Any thoughts on which one is better? Maybe depending on different scenarios?
Thanks.
EDIT
Pointed out by @JimmyJames that I missed the static
accessor on the methods.
I do not think this is related to my question. I should probably phrase my question better, these PARAMETER
are more likeCONSTANTS
, making them static
or even final
makes things easier to access directly like doOperation(Wrapper.ANOTHER_PARAMETER)
or doOperation(Wrapper.getParameter())
.
After reading comments and answer of @Robert Harvey and @JimmyJames, and other readings, I think essentially this is a bad practice that should be avoided (using class to hold constants).
Where do you keep Constants used throughout your application?
MORE QUESTION
What if there are moderate amounts of PARAMETERS/CONSTANTS
that are used across a module and they cannot be pinpointed to specific classes, at this time should we consider grouping all these together in a class, and access them through the methods mentioned above(as they will be available when class is loaded)?
static
class in this manner at all, that you should use an ordinary class with simple dependency injection or a dependency injection framework instead.// This private static int PARAMETER = 100;
- and question further proceeds asking to compare:// Or this? public static int ANOTHER_PARAMETER = 200;
Just the same as like the code in duplicate. Maybe you are confused by duplicate calling it variables instead of members or as they sometimes say fields?