我遇到了与变量“totalFloat”有关的私有(private)访问错误。然而,我一直在引用和使用同一个类中的方法,没有出现这个问题。
在这种情况下有没有通用的方法来解决私有(private)访问错误?
public static void writeHtmlFile() {
double contentsChangeDraw;
String ChangeDrawer = "ChangeFloat.html";
try {
PrintWriter outputStream = new PrintWriter(ChangeDrawer);
contentsChangeDraw=cd.getTotalFloat(totalFloat);
// totalFloat has private access in ChangeDrawer, which means I am
// unable to use the method to calculate the array that needs to be written
outputStream.println(contentsChangeDraw);
outputStream.close();
System.out.println("The contents of the ChangeDrawer have been written to a file");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
最佳答案
我敢打赌totalFloat
有这样的声明:
private int totalFloat; // or double, or boolean, or something else
您无法从静态方法访问它,因为它是成员变量。做到这一点
private static int totalFloat; // or double, or boolean, or something else
访问它(或将您的方法writeHtmlFile()
更改为非静态)。
关于java - 私有(private)访问? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665025/