Java – how to share a constant variable for all classes in the package?
•
Java
I want a constant variable to be common to all classes in the package Is there any way I can do this instead of using only one of them to define the interface and make each class implement it?
Solution
In Java, all constants must reside in a type (class or interface) However, you do not have to implement interfaces to use constants declared internally
You can try to add something like this to your bag:
interface Constants { static final String CONSTANT = "CONTANT"; }
Then use it like this:
String myVar = Constants.CONSTANT;
In this way, you still have your interface, but no class implements it
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
二维码