简体   繁体   English

在Java中设置全局变量

[英]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 并使用GlobalVar.email在任何地方访问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. 我的问题是,电子邮件的值不能设置为静态,因为它来自文本文件(来自使用Java properties class键/属性),并且如果将变量设置为static,我仍然看不到从txt文件加载值。

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. 我以为该解决方案会起作用,但是问题是,如果txt文件中不存在先前的值,则该值会在启动时动态生成。 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). 否则,将从txt文件(该程序在上一次运行时生成)中检索该值。

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. 如果我实施此解决方案,那么这是您首次运行该程序时,电子邮件的值将等于null(在整个程序中)。

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 ): 要初始化静态变量,可以使用static (请参见static初始化块 ):

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() . 只需将您的GlobalVar类设置为具有getter和setter,当您从正在执行的加载方法中读取文件时,请调用GlobalVar.setEmail("whatever") ,然后可以从任何位置使用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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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