【发布时间】:2013-12-02 12:12:41
【问题描述】:
我试图在我的 main 方法中从 OpenFile 类调用我的 createFile 方法,但我不断收到错误消息,说我无法从静态上下文调用非静态变量。
我确实尝试在我的主要方法中调用OpenFile of = new OpenFile();,但这不起作用,所以我目前在我的主要方法上方声明OpenFile,它工作正常,但是每次我尝试使用OpenFile 之一s 方法我得到同样的错误。
我尝试使用 static 开头一些东西,但这只会导致我的 IDE 显示错误的 sym 类型错误,我认为这是由导致其他错误的任何原因引起的。
这是来自OpenFile的createFile:
public class OpenFile {
private Formatter file;
public void createFile() throws FileNotFoundException{
try{
file = new Formatter("test.txt");
} catch(Exception e) {
System.out.println("Error creating file.");
}
}
这是我的主要方法:
OpenFile of = new OpenFile();
public static void main(String[] args) {
// TODO code application logic here
of.createFile();
intro();
createAndShowRibbon();
createAndShowNormalUI();
}
它与格式化程序有关吗?我以前从来没有用过。
谢谢。
【问题讨论】:
-
请先搜索。与
Formatter类型无关。 -
太基础的问题,需要先探索一下我也觉得:(.
标签: java static non-static