Can not make static reference to non static method? [duplicate]

廉价感情. 提交于 2019-12-04 22:23:20

You need to create instance of Start class and call gameStart() method on that instance because gameStart() is instance method not static method.

 public void main(String args[]) {
       new Start().gameStart();
       ..................
  }

Only static methods can be accessed by using class name as perfix.

first of all the main method should be

public static void main(String args[]) {

}

I assume you can have multiple games, and hence you should be able to start multiple instances so you should create a non static class that can be created and then actions performed against.

to answer your original question, you need to have a static variable that have static getters and setters..

public class Start {
    private static int menuResult = 3;
    public static int gameStart() 
    {
        return menuResult;
    }
    public static int getMenuResult()
    {
        return Start.menuResult;
    }
public int gameStart() <--- Instance method not static method

call it on instance

 Start start = new Start();
 start.gameStart();

So finally your classes should look like below

public static void main(String args[]) {
    Start start = new Start();
    start.gameStart();
    System.out.println(start.getMenuResult());
}

public class Start {
   private int menuResult = 3;
    public int gameStart() {
        return this.menuResult;//Don't know why there are two methods
    }
    public int getMenuResult() {
        return this.menuResult;
    }
}

If you need the method to be static, just add the keyword static in the function definition. That would get rid of the error. But if you want to keep the class Start the way it is, then you should create an instance of Start in the main function and then call the method. Hope that helps!

you are trying to invoke a method on its class name. you should be creating a new object and invoke its method

public void main(String args[]) {
        new Start().gameStart();
        System.out.println(menuResult);
    }

Start.gameStart() refers to a method which would be public static int gameStart() because Start is the class and not an instance of the object.

If you declare a method on an object, you need to apply it to instance of the object and not its class.

When to use static or instanciated methods ?

  • instanciated : whenever you need to apply the method to the object you're in. example : mycake.cook();
  • static : when the actions you do inside your method have nothing to do with an object in particular. example : Cake.throwThemAll();

mycake is an instance of a Cake, declared this way : Cake mycake = new Cake();

Cake is the class representing the object.

You should, i guess, have a read at some object oriented programmation course if you still have a doubt about objects, classes and instances.

While Other answers are Correct , that remains the Question that Why you Can't access Instance method Directly from Class name , In Java all static (methods , fields) bind with Class Name and when Class Is Loading to the Memory (Stack) all static members are Loading to the Stack , and this time Instance Method is not visible to Class. instance Method will Load into Heap portion in the memory and can only be access by Object references .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!