0

I've just started learning Java after getting the hang of C++, and I'm wondering if there is a way that I can have global constants the same way in C you can have a Header file with a list of constants that you can just include in any class files that need to use them. Right now, in my Java project, I am achieving this effect by using a Java interface with the constants defined as final variables.

public interface Constants {
    public int constant1 = 36;
    public String projectName = "Project Name";
    public float ratio = 4.716f;
    // ... more parameters
}

To access them in my class files that they are used in, I just use the implements keyword in the class files.

public class Class1 implements Constants {
    // Members
    // ...
    // Methods
    // ...
}

It's not that this isn't working, I'm just wondering if this is the best way to get this effect. I'm not formally trained in Java, I'm just teaching myself from some books I've gotten and online forums. I've looked around and I haven't found much discussion on this specific topic, more just about declaring constants within classes, so I'm just trying to figure out from some more experienced Java programmers with formal training if there is a more standard/conventional way to do this. Any advice would be helpful, thank you!

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • Use `public static final int constant1 = 36;` then you can use `Constants.constant1` in other classes. Edit: Unless you only want classes that extend from `Constants` to have access to it, in which case use `protected static final int constant1 = 36;` – dustytrash Sep 17 '18 at 17:17
  • 1
    Also see [this](https://stackoverflow.com/questions/2659593/what-is-the-use-of-interface-constants). – Andrew S Sep 17 '18 at 17:19
  • Thank you, this is what I was looking for. Apparently the better way to achieve this effect is to use a final constant class and import it into the desired class file that needs to access the constants. – alexsylvanus Sep 17 '18 at 17:31
  • 1
    `Enum` exists for a reason. Interface constants is an anti-pattern at best. –  Sep 17 '18 at 17:45
  • Possible duplicate of [What is the use of interface constants?](https://stackoverflow.com/q/2659593/608639) – jww Sep 17 '18 at 17:59

2 Answers2

2

Usually constants placed in a subject-related class as a class field. Here are examples from Java's Math class and Android's View

public final class Math {
    public static final double PI = 3.14159265358979323846;

public class View
    public static final int VISIBLE = 0x00000000;
    public static final int INVISIBLE = 0x00000004;

Then they can be used in any other classes with the class name prefixed or statically imported

double radius = 2 * r * Math.PI;

import static android.view.View.VISIBLE;
view.setVisibility(VISIBLE);

Interface constants is an anti-pattern and should be avoided. I'm not very experienced Java developer but haven't seen so far a class specifically dedicated to hold constants (except when dealing with resource bundles like Android's R classes).


Besides that, Java provides such a functionality as enums. There is the Item 34 Use enums instead of int constants in Effective Java by Joshua Bloch who is a designer and implementer of numerous Java platform features. So cannot add something new to this topic.

Maxim
  • 1,194
  • 11
  • 24
  • Enums aren't usable in this case as each constant has different type. – Jędrzej Dudkiewicz Sep 17 '18 at 18:54
  • Still enums can be used to implement global constants used between multiply public classes. I assume that the list of constants in the question is incomplete. – Maxim Sep 17 '18 at 18:59
  • Yes and no. They certainly can be used, but Enum suggests that these values are strongly related (e.g. enum Color - all values are colors, not only integers or RGB triplets) and that they are of the same type. So in case of this question you'd have to create as many enums as there are constants for this solution to be "clean" from design's point of view. You can, but shouldn't, create e.g. enum StringConstants with values ProjectName, VersionNumberAsString, AuthorName and so on. – Jędrzej Dudkiewicz Sep 17 '18 at 19:06
  • 1
    Agree and will update my answer in case someone will be interested in it. Or at least the question will be released from on hold state. – Maxim Sep 17 '18 at 19:14
0

Create separate class like below

public final class Constants{
    public static final int CONSTANT_1 = 36;
    public static final String PROJECT_NAME = "Project Name";
}

Use like below in the code

 Constants.CONSTANT_1;
Pandey Amit
  • 657
  • 6
  • 19