2

I have 3 different classes that both use some constant values. Currently, I'm defining all of them for each of the class. How do I centralize them?

I tried to create a new static class with just the constants, and then use static import in my classes, but that didn't work. The class could not be imported successfully.

PS: I don't want to use enum and interface.

Karan Goel
  • 1,117
  • 1
  • 12
  • 24

1 Answers1

1

You cannot import classes from the default package (i.e. the /src directory). You need to include the package name in the import clause, even if both classes are in the same package.

For instance:

  • Class Main in /src
  • Class Constants in /src/utils

In class Main:

 import static utils.Constants.*;
 class Main {...}

In class Constants

 package utils.Constants;
 class Constants {...}
Javier
  • 12,100
  • 5
  • 46
  • 57