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

问题描述 投票:-1回答:1
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此令牌

java global-variables
1个回答
0
投票

更改为

public static String path = "E:\\java_file\\AccountData.txt";
public static File myfile = new File(path);
© www.soinside.com 2019 - 2024. All rights reserved.