Is there any way to force a class to use the public static final field in Java?
•
Java
Is there any way to force classes in Java to have public static final fields (through interfaces or abstract classes)? Or at least just a public sphere?
I need to identify a set of courses in some way
public static final String TYPE =“…”;
Among them
Solution
No, you can't
You can only force them to use a non static getter method that will return the appropriate value for each subclass:
public abstract String getType();
If you need to map each subclass of a subclass to a value without instantiating it, you can create a public static map < < class , String > type; Somewhere, fill it statically with all classes and their types, and call typesholder types. Get (someclass. Class) get type
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码