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

后端 未结 9 1102
误落风尘
误落风尘 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:05

    annotations could be fine for your purpose.

    @FileProperties(desc="data file")
    public class DataFile extends XFile { ... }
    
    FileProperties props = DataFile.class.getAnnotation(FileProperties.class);
    String desc = props.desc(); 
    

    Accessing the info still requires reflection, however it's a little better than using static field/method.

    Java compiler does not enforce that all subclasses are annotated as such. You can add your logic to the compiler (using annotation processing) but that's too complicated. It's ok to check it at runtime.

    Update:

    This is also possible:

    @FileInfoClass ( DataFileInfo.class )
    @public class DataFile
    

提交回复
热议问题