-1

I have developed a Java Program which contains certain static variables that are used in all the classes.

Now, I want to store those variables at a single platform, which I can refer to call from any class rather than declaring them again & again.

I have researched on the internet and found some good solutions like declaring them in an interface and implementing that interface in my classes.

Also, I can write a separate class and call those variables using the object of that class.

Can anyone suggest the better solution? or which one is better between these two? please explain :)

Jawad-Dev
  • 274
  • 10
  • 31

4 Answers4

3

It kind of depends on what you mean by "variables" if you mean global mutable state, you should probably either use a Singleton pattern, or even better, redesign your application in such a way as to eliminate global mutable state. (Encapsulation)

If all your static variables are final, or effectively final (for instance loaded from environment variables at run time), then wrapping them in a class (with a private Constructor, since you do not want to be able to create objects of the class that only has static members) is probably the best solution.

Interfaces are designed to describe behavior, rather than state, so unless you have a contract described in it, it doesn't seem useful to have it as an interface, and could potentially lead to confusion down the road.

Mikkel Løkke
  • 3,710
  • 23
  • 37
  • Impressive. I have some variables whose value will be assigned in the class but I want to declare them once with initialization. They are not final. So what should I follow from above? – Jawad-Dev Mar 02 '20 at 12:49
  • If they are not final, you should use the [Singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern), since that gives you some control over critical sections and concurrency. That being said I would really recommend against using global mutable state, as it is generally considered an anti-pattern. – Mikkel Løkke Mar 02 '20 at 14:09
2

1:- Create an enum class like this,

    public enum YourClassName{

 // these are your three varible
VARIABLE1("val1"), VARIABLE2("val2"), VARIABLE3("val3"),;

private String value;

YourClassName(String value) {
    this.value=value;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

}

2:- Once you have an enum class with some variable then you can call from every class like this,

 YourClassName.VARBIABLE1.getValue();

this will help you and i think its a good way.

Fazle Subhan
  • 211
  • 3
  • 5
  • It will make it more complicated, I am using public static String VARIABLE just want to call this variables .Setters and getters will make it more complex – Jawad-Dev Mar 02 '20 at 12:15
  • Take care of java naming conventions. Class name should start with upper case character. Enum values like your *varable1* should be upper case – Jens Mar 02 '20 at 12:22
1

You can refer to static variables from any class... (assuming they are not private): just do ClassName.variable.

The better option is to not use static variables as global variables. Use getters and setters to pass values.

The interfaces solution is hacky and I have used it before - but that is also not ideal. Implementing a constants interface is considered bad practice.

sleepToken
  • 1,866
  • 1
  • 14
  • 23
  • What do you mean by implementing a constant interface, because you dont have to implement that interface to access those static variables, just asking for my clarity – DevX Mar 02 '20 at 12:07
  • So where should I declare them? Shall I create a new class for constants and declare my variables there? – Jawad-Dev Mar 02 '20 at 12:08
  • @DevX No, you don't have to, but that's what OP is trying ("declaring them in an interface and implementing that interface in my classes") – tobias_k Mar 02 '20 at 12:08
  • 1
    @DevX I assumed the OP was talking about https://stackoverflow.com/questions/320588/interfaces-with-static-fields-in-java-for-sharing-constants – sleepToken Mar 02 '20 at 12:08
  • @sleepToken yes that is what I thought but not sure why is he looking to implement that because in most scenarios we have a separate interface for constants and hardly implement them ever – DevX Mar 02 '20 at 12:11
  • @Devx can you please elaborate your answer? I am finding it useful – Jawad-Dev Mar 02 '20 at 12:12
  • @Jawad-Dev you can [see](https://stackoverflow.com/a/60488979/1722792) the answer explanation – DevX Mar 02 '20 at 12:56
1

Well to further work on @sleepToken answer there can be multiple ways to what you are looking to do. Again these approaches may depend on the design and at times may totally be different than what is being followed in the code base that you are working on.

For instance in most cases I have seen a convention that constants are defined in an Interface in java and that interface is hardly ever implemented. You can have multiple of these interfaces as well. Like one interface for storing constants related to user and may be another for the constants related to the system

  • IUserConstants.java
  • ISystemConstants.java

Now this is totally up to you to decide the names and what these interfaces will hold. Since these are constants you will define in them they are supposed to be public and static and at times you will make them final as well but make sure that you know the difference between runtime and compile time constants. Again this is something you will have to decide about. Further more public static final are redundant for interfaces. This link just got my attention.

You can check this sample interface

public interface IConstants{
public static final int TIME=10; 
public static final String CONFIG_STRING=SomeClass.LoadValue(); 
}

Another approach I have seen is to use an abstract class that contains all the constants which are static and public and if you want to make sure that it is not further used for implementation you can use a private constructor although I am not sure why this was used because at times legacy code that comes in front hardly makes sense.

public abstract class IConstants
{

  public static final int TIME = 10;
  public static final String CONFIG_STRING = SomeClass.LoadValue();constant

  private IConstants()
  {
    throw new AssertionError();
  }
}

I hope this helps you

DevX
  • 308
  • 5
  • 16