简体   繁体   中英

Can't Instantiate Field (instance variable) in main() method. Why?? Java

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";

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM