有人可以告诉我我在这里做错了什么吗?当我在 Eclipse 中有这段代码时,当我尝试将 sheetName
设置为 "hi"
时,它告诉我无法“对非静态字段进行静态引用”在主要方法中。我在这里做错了什么?我知道这一定很简单,但我到处找了也没搞明白!
public class AutoExpire {
private String sheetName;
private FileInputStream inputStream;
/**
* Instantiates the class.
*/
public AutoExpire() {
// do nothing
}
/**
* The main method from which the program is ran.
*
* @param args
* No arguments.
* @throws IOException
* If program fails to run.
*/
public static void main(String[] args) throws IOException {
sheetName = "hi";
最佳答案
main
方法是静态的,因此 main
方法中没有 AutoExpire
实例。创建一个实例,然后设置实例的字段。
public static void main(String[] args) throws IOException {
AutoExpire ae = new AutoExpire();
ae.sheetName = "hi";
关于java - 无法在 main() 方法中实例化字段(实例变量)。为什么?? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17181243/