简体   繁体   English

我可以在 JAVA 中用另一个全局变量初始化一个全局变量吗?

[英]Can I initialize a global variable by another global variable in JAVA?

static class Variable{
        public static File myfile;
        public static String path = "E:\\java_file\\AccountData.txt";
        Variable.myfile = new File(Variable.path);
}

when I trying to create a file like:当我尝试创建一个文件时:

protected static void FileCreate(){
    try { 
        if (Variable.myfile.createNewFile())
            System.out.println("File created"); 
        else
            System.out.println("File already exists");
    } catch (Exception e) { 
        System.err.println(e); 
    }
}

and it's appear an error:它出现了一个错误:

Syntax error on token "myfile", VariableDeclaratorId expected after this token令牌“myfile”上的语法错误,在此令牌之后需要 VariableDeclaratorId

Change to改成

public static String path = "E:\\java_file\\AccountData.txt";
public static File myfile = new File(path);

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

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