Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot make a static reference to the non-static method

 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I do that?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming reference is the first part of the following?:

Polygon polygon = new Polygon();
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess what I'm asking is "How can I activate an upper class's methods through a lower class?"
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21146
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic