0

I need to create a 100 or more static final constants in my application and I can achieve this is following two ways as per my understanding:

  1. Creating a simple java class and create static final field in that
  2. Creating an interface an put all variable in that because all field in an interface is implicitly static final

I have these question in above approach:

  1. Which one is right approach to achieve this?
  2. Which one is memory efficient approach?
  3. Is there any design pattern to achieve this?
Geek
  • 1,510
  • 14
  • 17

5 Answers5

6

You can refer to many books about the topic.

I will quote a good one: "Effective Java"

Item 19: Use interfaces only to define types

The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class’s exported API. It is of no consequence to the users of a class that the class implements a constant interface

you can even check where JDK mostly constants are declared..

Math.PI for example is declared in the class Math and not in an interface

and as an exception you can see constants like in the java.io.ObjectStreamConstants but again the Books are there to help:

From effective java again:

There are several constant interfaces in the Java platform libraries...

These interfaces should be regarded as anomalies and should not be emulated.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

I would not be thinking should they be in an interface or class, but more about the constants and their meaning.

I would not recommend putting all your constants in one place for the sake of keeping them together. If for instance a constant is directly related to a class then would say put it in that class. I have worked with code where all the constants ate bundled into one class, and I don't thing it is a good approach.

MartinByers
  • 1,240
  • 1
  • 9
  • 15
1

Have you considered approach with ENUM or it doesn't fit in your case? I think, the approach with ENUM can gives you some benefits over constants.

Why use Enums instead of Constants?

i.merkurev
  • 465
  • 2
  • 8
0

I think that convenient way is to keep them in one place, if they are have common nature. Anyway, they should be grouped by some attribute. You can create class for them like this:

public final class Consts {
    public static class GroupA {...}
    public static class GroupB {...}
    //and so on
}

With groups this class becomes much readable and a little bit better manageable. About memory consumption, try to use primitives for your constants, because they do not require additional space for meta information.

eg04lt3r
  • 2,467
  • 14
  • 19
0

You can create Final or static constraint much as you like just by declaring field inside interface class so i would like to go with your option number 2

  • 1
    Hello, welcome to StackOverflow! I highly recommend you to provide an example as a guide to your answer, so you can enfact that you posting a fact. – Alexander Santos Dec 21 '20 at 22:37