无法实例化main()方法中的字段(实例变量).为什么??爪哇 [英] Can't Instantiate Field (instance variable) in main() method. Why?? Java

查看:141
本文介绍了无法实例化main()方法中的字段(实例变量).为什么??爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我我在做什么错吗?当我在Eclipse中拥有此代码时,它告诉我尝试在main方法中将sheetName设置为"hi"时,无法对非静态字段进行静态引用".我在这里做错了什么?我知道它一定很简单,但是我到处搜索并且无法弄清楚!

Can someone tell me what I am doing wrong here? When I have this code in Eclipse, it is telling me I cannot "make a static reference to a non-static field" when I try to set sheetName to "hi" in the main method. What am I doing wrong here? I know it must be simple, but I searched everywhere and cannot figure it out!

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的实例.创建一个实例,然后设置实例的字段.

The main method is static, so you have no instances of AutoExpire in the main method. Create an instance, then set the instance's field.

public static void main(String[] args) throws IOException {
    AutoExpire ae = new AutoExpire();
    ae.sheetName = "hi";

这篇关于无法实例化main()方法中的字段(实例变量).为什么??爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆