Java: static abstract (again) - best practice how to work around

后端 未结 9 1121
误落风尘
误落风尘 2021-02-01 16:30

I theoretically understand the point why there is no abstract static in Java, as explained for instance in Why can't static methods be abstract in

9条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 17:11

    I don't know how a java guru would solve it, but I'd probably create a resource bundle with all the descriptions in a properties file like this:

    com.bigcompany.smallapp.files.DataFile=Data file
    com.bigcompany.smallapp.files.ConfigFile=Config file
    

    Handling the bundle can conveniently be placed in the superclass or elsewhere.

    Another option is to use reflection to access the static fields in each subclass, but then you need to make sure that all the subclasses have a static field with the same name.

    There could be other options too, even refactoring the code so that the subtypes aren't represented by a separate class each, but in general there's no watertight solution.

提交回复
热议问题