3

STYLE 1

public interface Constants{
    String FOO = "foo",
           BAR = "bar",
           ... ;
}

STYLE 2

public interface Constants{
    String FOO = "foo";
    String BAR = "bar";
    ...
}

I used to declare all the constants in a single interface. I am following style 1 because it's more readable. I am wondering if there's any difference between these two styles other than the readability. Is there any performance related things about these two styles?

NOTE : I never implemented the Constant interface, instead I accessed those constants like Constants.FOO.

example :

public class Main{

    public static void main(String[] args){

        ...
        System.out.println(Constants.FOO);
        ...

    }

}

Which is better, and why?

theapache64
  • 10,926
  • 9
  • 65
  • 108
  • 2
    Don't. It's an anti-pattern. https://en.wikipedia.org/wiki/Constant_interface – karakfa Aug 22 '15 at 13:02
  • 1
    In general is used enum type or "private final static" in a class. – Mihai8 Aug 22 '15 at 13:04
  • 1
    It's generally discouraged to declare several variables at once. The second one is actually more readable to me: I can find the type of the variable on the line of the variable, and not 25 lines above. The compiled byte-code would be identical. – JB Nizet Aug 22 '15 at 13:05

4 Answers4

4

I am following style 1 because it's more readable.

Consider following case,

 private static final String FOO1="foo1", FOO2="foo2", FOO3="foo3",BAR="bar"...//and so on

Now this is quite not readable actually than check following,

private static final String FOO1 ="foo1";
private static final String FOO2 ="foo2";
private static final String FOO3 ="foo3";//and so on

Now consider readbility of above code,

constant FOO1 ="foo1";
constant FOO2 ="foo2";
constant FOO3 ="foo3";//and so on

and

constant FOO1 = "foo1",  FOO2 = "foo2", FOO3 = "foo3";

While reading second one you may miss the point that FOO3 is also constant while in first case it's quite noticeable for anyone that FOO3 is constant. Moreover in both the cases there is no difference in memory or execution.

If you are specifically creating interface for constants than you are not using interface for which purpose it is there in Java. Interface should contain some contract on which implementing class must agree. Use of interface to contain the constant only is unpleasant.

akash
  • 22,664
  • 11
  • 59
  • 87
2

They're exactly the same at the compiled code level. No difference whatsoever. However, try to avoid such a pattern. Several answers have been given before explaining why it's not encouraged.

If you don't intend to implement the constants interface, it would make more sense to at least place the constants in a final class.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Inside an interface, every field is implicitely final and static. But I agree they shouldn't be in an interface in the first place. – JB Nizet Aug 22 '15 at 13:06
  • I think every field in an interface is automatically public static and final . isn't? – theapache64 Aug 22 '15 at 13:07
  • I never implemented that interface, instead I accessed that constants like `Constants.FOO`. – theapache64 Aug 22 '15 at 13:09
  • @ShifarShifz Then why not simply put them in a class at least, instead of an interface? And what about enums? – M A Aug 22 '15 at 13:21
2

You should not define constants in an interface at all. It is bad practice. Instead define each constant in the class where it is logically appropriate.

Additionally, such constants should be static final. This way you will only need one instance of a constant, instead of defining new ones all over the code, which affects performance.

Edit: as noted in comments, interface constants are static final by definition. However, when properly defined where they should be, it is worth to note this as a reminder.

Community
  • 1
  • 1
milez
  • 2,201
  • 12
  • 31
1

First in place declare all the constants in it's own class. Do not group them in a single interface, that's a bad design.

I am wondering if there's any difference between these two styles other than the readability.

No difference other than readability.

Is there any performance related things about these two styles?

No. Same. Compiles down to same byte code.

Which is more better, and why? .

Best only in terms on readability only. Again, prefer the best readable way to you. I prefer second.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • but suresh, the constants are used in multiple classes. and I never implemented that interface, i used to call `Constants.FOO`. is it now a bad practice? – theapache64 Aug 22 '15 at 13:11
  • 1
    If the interface is not meant to be implementedn then why make it an interface. Make it a final class, with a private constructor. Your code will work as fine, and nobody will be tempted to implement that interface, since it won't be one. Besides, grouping unrelated constants in a single class is also bad practice. Put the constants in the class where they belong. For example, BorderLayout constraints are defined in BorderLayout. Integer max and min value are defined in Integer. – JB Nizet Aug 22 '15 at 13:14
  • 1
    When you see `Constants.FOO`, If `FOO` is a static constant, `Constants` not necessarily an interface. – Suresh Atta Aug 22 '15 at 13:15
  • @JBNizet Thank you for those valuable informations jb. – theapache64 Aug 22 '15 at 13:18