简体   繁体   中英

Java, static variable doesn't initialize from outside of class

I have a class that all of its methods and variables are static. Class is like this:

public class LinkManager {

    private static final String TAG = "LinkManager";
    private static final String UAT = "http://uat.MY-Domain.com/";
    private static final String PRODUCTION = "https://www.MY-Domain.com/api/";

    private static String DOMAIN;

    private static final String FACEBOOK_PROFILE_IMAGE_URL = "http://graph.facebook.com/#@#/picture?type=large";
    private static final String FACEBOOK_WALL_URL = "https://graph.facebook.com/#@#/feed";

    private static final String URL_LOGIN         = DOMAIN + "login/";
    private static final String URL_USER_PROFILE  = DOMAIN + "user/";
    private static final String URL_VENUE_LIST    = DOMAIN + "venues/?centre_lat=#@#&centre_lon=#@@#&radius=#@@@#";
.
.
.

    public static void setBackendMode(int mode) {
        switch(mode) {
            case 0:
                DOMAIN = PRODUCTION;
                Log.i(TAG, "Backend mode: Production");
                break;
            case 1:
                DOMAIN = UAT;
                Log.i(TAG, "Backend mode: UAT");
                break;
            default:
                Log.e(TAG, "Fatal Error!!! Check you backend url. Selected mode is: " + mode);
        }
    }

    public static String getFacebookProfileImageUrl(String userId) {
        String str = FACEBOOK_PROFILE_IMAGE_URL;
        str = str.replaceAll("#@#", userId);

        return str;
    }

    public static String getFacebookWallUrl(String userId) {
        String str = FACEBOOK_WALL_URL;
        str = str.replaceAll("#@#", userId);

        return str;
    }

    public static String getLoginUrl() {
        return URL_LOGIN;
    }

    public static String getUserProfileUrl() {
        return URL_USER_PROFILE;
    }

    public static String getVenueListUrl(String lat, String lng, String radius) {
        String str = URL_VENUE_LIST;
        str = str.replaceAll("#@#", lat);
        str = str.replaceAll("#@@#", lng);
        str = str.replaceAll("#@@@#", radius);

        return str;
    }
.
.
.
} 

I have another class which is my main class. I want to initialize DOMAIN variable through this class. Since, I'm developing application for Android, onCreate() is the first method that invokes. This is my code:

public class MainScreen extends FragmentActivity {

    private static final String TAG = "MainScreen";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(TAG, "*********************************");
        Log.i(TAG, "Try to run ------- application...");
        Log.i(TAG, "*********************************");

        // Set Backend mode (Production or UAT)
        LinkManager.setBackendMode(1);

        ...
    }
}

Now, when I run the application log shows this output:

06-28 10:00:56.973: I/MainScreen(9014): *********************************
06-28 10:00:56.973: I/MainScreen(9014): Try to run ------- application...
06-28 10:00:56.973: I/MainScreen(9014): *********************************
06-28 10:00:56.973: I/LinkManager(9014): Backend mode: UAT
06-28 10:00:57.023: I/ContentDownloader(9014): Try to open=> nulluser/

Line 4 shows DOMAIN variable has initialized with UAT but after that when other classes invoke methods of LinkManager class DOMAIN contains null. Since DOMAIN is static variable I expect content of this variable should not be changed.

What/where is my mistake? Any suggestion would be appreciated. Thanks

After you invoke setBackendMode() and before setBackendMode() is really executed, JVM will initialize URL_LOGIN, URL_USER_PROFILE, URL_VENUE_LIST , when DOMAIN is still null .

Solution:

Just do this:

public static String getLoginUrl() {
    return DOMAIN + "login/";
}
public static String getUserProfileUrl() {
    return DOMAIN + "user/";
}
public static String getVenueListUrl(String lat, String lng, String radius) {
    String str = DOMAIN + "venues/?centre_lat=#@#&centre_lon=#@@#&radius=#@@@#";
    // ...
}

It is because

private static final String URL_USER_PROFILE  = DOMAIN + "user/";

Is executed before DOMAIN is set in setBackendMode()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM