我可以通过JAVA中的另一个全局变量来初始化全局变量吗?

时间:2019-12-13 05:48:03

标签: java global-variables

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

当我尝试创建如下文件时:

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); 
    }
}

出现错误:

  

令牌“ myfile”的语法错误,预计在之后出现VariableDeclaratorId   这个令牌

1 个答案:

答案 0 :(得分:0)

更改为

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