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.

2
  • Well you can always use a static import but, at some point, you will need to specify the fully classified name. Commented Jun 4, 2016 at 18:57
  • 1
    How about using top-level enum instead? Commented Jun 4, 2016 at 18:59

2 Answers 2

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; 
Sign up to request clarification or add additional context in comments.

4 Comments

I need all consts of 'NewDeviceDetailsConsts', in around 10 classes,which one will you suggest ?
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.
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 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.
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; } 

3 Comments

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?
The problem with static imports 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.
If you are still looking for more information as to which variant to use here you will find some very good explanations why you should consider using enums. Not only the accepted answer but also others have very good explanations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.