问题
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";
回答1:
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";
来源:https://stackoverflow.com/questions/17181243/cant-instantiate-field-instance-variable-in-main-method-why-java