theapache64
theapache64

Reputation: 11734

Java Constants definition style

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?

Upvotes: 3

Views: 262

Answers (4)

Akash Thakare
Akash Thakare

Reputation: 22974

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.

Upvotes: 4

M A
M A

Reputation: 72844

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.

Upvotes: 2

milez
milez

Reputation: 2201

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.

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 1

Related Questions