A simple way to create static imports for constants in existing code?

前端 未结 9 1004
心在旅途
心在旅途 2021-01-30 21:17

If I have a constant BAR in Foo, which I will use in a class C I\'ll have to write

Object o = Foo.BAR + \"...\";

which I can use Ctrl-Shift-M i

9条回答
  •  别跟我提以往
    2021-01-30 21:43

    I don't know if it would fit your needs, but you can always have these constants defined in an interface and have all your classes just implement this interface. Then using some smart RegExp as others suggest you could get rid of all Foo. occurences.

    Example:

    public interface Foo {
       static final String constant1 = "Some";
       static final String constant2 = "Value";
       //...
    }
    

    And then

    public class YourClass implements Foo {
       public YourClass() {
          System.out.println("Hello " + constant1);
       }
    }
    

提交回复
热议问题