• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot make a static reference to the non-static method

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I do that?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming reference is the first part of the following?:

Polygon polygon = new Polygon();
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic