简体   繁体   中英

Setting Global Variables in Java

Here's my problem: I have a number of classes that need access to a specific value (such as an email address) and instead of declaring it again and again in each class I'd like to declare it once and access it using some sort of global variable.

Now before you naysayers start screaming (or down-voting this post) I am aware you could declare a class like this:

public class GlobalVar{

        public GlobalVar() throws Exception
    {

    }
    public static String email = "myemail@gmail.com";

 }

And access email anywhere by using GlobalVar.email

My problem is the value of email cannot be set as static because it comes from a text file (from key/property using Java properties class ) and I don't see anyway to load the value from txt file if I set the variable to static.

I am basically storing a dynamically generated email in a file and retrieving it the next time I start the application. Of course I can attempt to retrieve it each time I need to use the variable but this is not elegant.

Update:

A potential solution has been proposed as follows:

public static String email = null;
static {
    email = "awesome@email.com"; // change to your initialization process
}

I thought this solution would work however the problem is that the value is generated dynamically at startup if a previous value does not already exist in the txt file. Basically, if it is the 1st time you are running the application a new value is generated. Otherwise the value is retrieved from a txt file (which was generated at a previous run of the program).

If I implement this solution the value of email will be equal to null (across the entire program) if this is the first time you run the program.

I basically need a way to initialize a static variable using a conditional statement but I don't know if this is possible.

Thanks

To initialize a static variable you can use static (see static initialization blocks ):

public static String email = null;
static {
    email = "awesome@email.com"; // change to your initialization process
}

An alternative would be to use a static function:

class Mail {
    public static String email = initializeMailVariable();

    private static String initializeMailVariable() {
        // initialization code goes here
    }
}

Just set up your GlobalVar class to have getters and setters, when you read the file from whatever load method you're doing, call GlobalVar.setEmail("whatever") and then you can access it from anywhere with GlobalVar.getEmail() .

public class GlobalVar{

    private static String email = "";

    public static void setEmail(String email){
        this.email = email;
    }

    public static String getEmail(){
        return this.email;
    }
}

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