2

I created SomeConfig to store there static data. However I try to understand witch options is better (or none of both)

Before I had class SomeConfig written like:

public class SomeConfig {

    private static int mValue = 8;
    private static String mString = "some String";

    public static int getValue() {
        return mValue;
    }

    public static void setValue(int value) {
      mValue = value;
    }

    public static String getTheString() {
        return mString;
    }

    public static void setValue(String theString) {
       mString = theString;
    }
}

Now I changed it to:

 public class SomeConfig {
    private static SomeConfig mSomeConfig = new SomeConfig();

    private int mValue = 8;
    private String mString = "some String";

    public static int getValue() {
        return mSomeConfig.mValue;
    }

    public static void setValue(int value) {
        mSomeConfig.mValue = value;
    }

    public static String getTheString() {
        return mSomeConfig.mString;
    }

    public static void setValue(String theString) {
        mSomeConfig.mString = theString;
    }
}

Generally i changed private variables to non-static but API stays the same.

What is a difference between two options I posted?

Thanks,

4
  • read about static variables (docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) Commented Feb 28, 2014 at 17:49
  • static variable generally are PUBLIC STATIC variable because they are used just for temp values . for anything important use the platform provided arch (android sharedprefrence , for web dev session object etc) Commented Feb 28, 2014 at 17:52
  • Uh, your second code could (should?) be converted to a singleton, really Commented Feb 28, 2014 at 17:55
  • Both purpose you are implementing is different, it depends on the requirement what you need . Commented Feb 28, 2014 at 17:58

4 Answers 4

1

If you want only one instance of your SomeConfig to exist in your application then you might want to make it a Singleton class. Refer to this link : link

Your second option seems to be the closest to being a Singleton, you just need to make your Default constructor Private to ensure that no other class can create another instance of SomeConfig.

Sign up to request clarification or add additional context in comments.

2 Comments

in second option i have getter/seeter that fetches/stores data by using one instance
BUt there is no check to stop me from creatring another instance of Someconfig class. The user of your class will not be clear of what the purpose is, but you can enforce this by making your constructor Private
1

As per my understanding static variables are class variable and those are not require any object for calling or assigning value .The values for those static variables are remains same over the class.Once you assign a value, all object can access that value.

Hope it will help you.

Comments

1

Generally, I think it's a good practice to avoid static variables and methods, unless there is a real need (I guess common use of static is "utility" type method, or constants etc). If you do not want to instantiate the class multiple times or want to ensure single instance of the configuration, I think implementing it as a singleton would be a better way to go here.

Comments

0

I wouldn't recommend using any of the two for configuration purposes.

The difference between these two are just that one uses an instance to hold the values, the other uses static variables.

You might look into having a configuration class that utilises a ResourceBundle to load the values from a .properties file during initialisation.

1 Comment

Thanks, in my case (Android application) I load data from Preferences and store it to Config file for the future use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.