aspose file tools*
The moose likes Beginning Java and the fly likes Cannot make a static reference to the non-static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot make a static reference to the non-static method" Watch "Cannot make a static reference to the non-static method" New topic
Author

Cannot make a static reference to the non-static method

Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

Here is my problem.

I have a main class, GameStart.java.

I want to run code in a sub class named MenuState.java, then when the code is finished, I want MenuState to call upon one of GameStart's methods.

However, if I try and do then Eclipse gives me this error: Cannot make a static reference to the non-static method enterState(int) from the type StateBasedGame(The superclass)

How can I make it so that MenuState can run GameStart's method without the static reference error?

Thanks in advance.


"The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!', but 'That's funny...' "
- Isaac Asimov
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Do you understand what 'static' means? It's explained here: Understanding Instance and Class Members.

You're calling the enterState method on class GameStart as if it's a static method. If it's not a static method, you should call it on an instance (an object) of class GameStart, instead of on the class itself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I understand what static means. However, the problem is that when a GameStart object is created, it creates a new MenuState. Therefore, creating a new GameStart in that MenuState will cause a stack overflow.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2409
    
  28

Gamestart should probably give it's own reference to MenuStart then. Although having MenuStart call methods on an GameStart that isn't completely initialized is kind of hokey


Edited: You might want to think about the lifecycle of your objects a little more. Ideally, your app should go through a initialization phase in which your object tree is all initialized, and then a "start" phase in which the differrent things are started. This prevents you from running into a problem where you start doing something before other things are setup correctly.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

How do I do that?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Alix Ollivier wrote:Here is my problem. . . .

I want to run code in a sub class named MenuState.java, then when the code is finished, I want MenuState to call upon one of GameStart's methods.
. . .
That sounds peculiar, too. What does it mean? Please explain more. You may have some poor design causing the multiple object creations.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2409
    
  28

How do I do what? Pass a reference? or get your own reference? or call a method by an object's reference?

DO you understand what reference means?
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I have a main class called GameStart. Now, in video games, in order to hold the different types of scenes, like menu, actual gameplay, cutscenes, and etc, you have things called states. Each state is an individual set of code that is not affected when it is not on. When I start GameStart, it automatically switches to my first state, MenuState. When I am done with the menu, and the player clicks the "continue" button, I want MenuState to tell GameStart to switch the state to GameplayState. I would do it in MenuState if I could, but the only thing I could find in the library I am using is something to leave the state, but nothing to switch them. And I also can't just tell GameStart to update until it gets a leave notification, because the update() method in GameStart is a final protected method, and I don't want to change the source code of my library.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I'm assuming reference is the first part of the following?:

Polygon polygon = new Polygon();
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
No, it is the second part polygon with a small p.
I think Jayesh meant it as in this contextThat line contains two references: polygon and myApp.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I guess what I'm asking is "How can I activate an upper class's methods through a lower class?"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
They are not called upper and lower, but superclass and subclass.
Remember that non-private members of a class are inherited by objects of its subclass:-If the method has been overridden, then you are not intended to call the superclass version from a subclass reference.

I still don’t understand the problem about the game. Sorry.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Alix Ollivier wrote:I have a main class called GameStart. Now, in video games, in order to hold the different types of scenes, like menu, actual gameplay, cutscenes, and etc, you have things called states. Each state is an individual set of code that is not affected when it is not on. When I start GameStart, it automatically switches to my first state, MenuState. When I am done with the menu, and the player clicks the "continue" button, I want MenuState to tell GameStart to switch the state to GameplayState.


I'm not sure I like that design. Should the State objects really be telling the GameStart object (I don't like that name either, it should just be Game, I think) what to do? Shouldn't the button be the one doing that? I'm thinking of a controller layer which keeps track of what states are active and, when necessary, activates or inactives the states.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cannot make a static reference to the non-static method