Need an advice on how to achieve decoupling of UI graphs from content.
Preamble:
I have an abstract Sensor interface and a bunch of implementations. Interface defines a couple of functions, such as parsing text to actual values, etc.
On the other hand, each Sensor implementation has it's own defined maximum range, which is final constant (and, in sense, static). On the user interface I would like to draw graphs of incoming Sensor data. Each graph has to have default Y range set to maximum range of the particular implementation.
Actual question
Is there a way of accessing static fields of implementations via abstract superclass or interface without actually instantiating a dummy object of specific implementation type?
1 Answer 1
Static methods in Java are not for abstract access.
In some languages we have true metaclasses, meaning that classes themselves are represented by full-fledged instances. Metaclasses can house class (i.e. static in Java) methods & fields but (unlike Java with statics) they can also participate in class hierarchy, meaning we can create abstraction over several metaclass instances (i.e. over several regular classes).
In Java, we don't have that so if you want abstraction at the class level, we just use other regular classes that are more loosely associated (rather than specifically one class being the metaclass for another class).
Given Java, there are three choices, I think. Use statics without abstraction, conflate this capability with another existing abstraction already in your design, or create another separate but related abstraction for this, say one that represents configuration, or a metaclass, or factory, or something else.
MyClass.Member;
no dummy object required. See caveofprogramming.com/java/…