0
public final class Templates {
  public static class NewDeviceDetailsConsts {
        public static final String AAA = "aaaa";
        public static final String BBB = "bbbb";
        public static final String CCC = "cccc";
   }
   }

for using AAA, I have to write Templates.NewDeviceDetailsConsts.AAA and thats a long string to use 10-20 times in every class I use it.

Will it be efficient to use it like, I define a field in classes I need it , Templates.NewDeviceDetailsConsts DeviceConst; and use DeviceConst.___ in the class. Is it fine or can I do it better than that.

tarun14110
  • 940
  • 5
  • 26
  • 57

2 Answers2

2

There a a few ways you can solve this problem.

  1. create a static import:

    Where your import statements are, add this import static path.to.Templates.NewDeviceDetailsConsts.AAA;. This will allow you to reference your AAA object just by typing AAA.

    Unfortunately, you will have to add this line at the top of all your classes.

  2. Create a getter your Templates class.

    public static NewDeviceDetailsConsts getAAA(){
        return NewDeviceDetailsConsts.AAA;
    }
    

    Then use Templates.getAAA() to get the AAA object.

  3. Save a reference to the AAA object inside the working class.

    private static NewDeviceDetailsConsts AAA = Templates.NewDeviceDetailsConsts.AAA;
    
Luke Melaia
  • 1,470
  • 14
  • 22
  • I need all consts of 'NewDeviceDetailsConsts', in around 10 classes,which one will you suggest ? – tarun14110 Jun 04 '16 at 19:12
  • Seeing as how you need to reference the objects in multiple classes, create the getters in your `Template` class. This will allow you to access the objects no matter what class you're working in. – Luke Melaia Jun 04 '16 at 19:14
  • I came up with this `import static Templates.NewDeviceDetailsConsts.*`. I will use this and use the Consts directly. Is this approach better or should I go with getters ? – tarun14110 Jun 04 '16 at 19:55
  • @tarun14110 that approach works perfectly well. The only drawback is you will have to add that at the top of each file, otherwise it's completely acceptable. – Luke Melaia Jun 04 '16 at 19:56
1

I would use enum's for such purpose:

public final class Templates {
    public enum NewDeviceDetailsConsts {
        AAA("aaaa"), BBB("bbbb"), CCC("cccc");

        private String value;

        private NewDeviceDetailsConsts(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }
}

Then you can use the constants as follows:

NewDeviceDetailsConsts aaa = NewDeviceDetailsConsts.AAA;

You can also use AAA, BBB, ... without param if you need as follows:

public final class Templates {
    public enum NewDeviceDetailsConsts {
        AAAA, BBBB, CCCC;
    }
}

And lastly, you should not define them inside a class. An enum can also be a top level class as follows unless they have to be part of a class:

public enum NewDeviceDetailsConsts {
    AAAA, BBBB, CCCC;
}
ujulu
  • 3,289
  • 2
  • 11
  • 14
  • I came up with this `import static Templates.NewDeviceDetailsConsts.*`. I will use this and use the Consts directly. Is this approach better or should I go with enums? – tarun14110 Jun 04 '16 at 19:55
  • The problem with `static import`s is that you pollute the namespace of your classes. Imagine you use them in lot of classes and let us assume the classes are more than a couple of lines long, then if you encounter a constant `AAA` in the code you do not directly see where it comes from. If you use them in class hierarchy they can cause error you might not see easily. As for me, I don't see any reason why I have to use static constants instead of `enums`. Enums are very powerful too. – ujulu Jun 04 '16 at 20:19
  • If you are still looking for more information as to which variant to use [here](http://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel) you will find some very good explanations why you should consider using `enum`s. Not only the accepted answer but also others have very good explanations. – ujulu Jun 04 '16 at 20:48