سؤال

So, in short. I have two classes.

package rpg;

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

and

package rpg;
public class Start {
    int menuResult = 3;
    public int gameStart() 
    {
        return menuResult;
    }
    public int getMenuResult()
    {
        return Start.menuResult;
    }
}

It keeps throwing up the error 'Cannot make static reference to non-static method gameStart()'. I'm sure I'm missing something simple, but can't find it. Thanks!

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

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

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 .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top