Re: Checking objects static or not

From: Steve Horsley (shoot_at_the.moon)
Date: 11/20/03


Date: Thu, 20 Nov 2003 20:17:06 +0000

fix wrote:
> Is there any simple way to check if an object is static or not?
>
> In addition, I appreciate if anyone could tell me the problem with the
> context.
> We are writing a game, I have a bunch of source file,
>
> MainMenu.java
> - extends JFrame
> - contains the main method which creates a static instance of itself
> and show to the user
> - has a "New game" button, after clicked, fires the actionPerformed method
> - in actionPerformed method, if-else to check if user hits the "New
> game" button, if yes, declare a Jframe and initialize it with the Game
> window (below).
> {
> JFrame gameFrame = new Game();
> frame.setVisible(false);
> }
>
> Game.java
> - extends JFrame
> - contains a GamePanel (below) which extends JPanel, written in another
> source file
> - when user click X, before exiting, the windowClosing event fires,
> which try to stop the timer start in the JPanel by calling a stopTimer()
> method in the GamePanel
>
> GamePanel.java
> - Starts the timer in the constructor
> - has a stopTimer method which simply stop the timer by timer.stop();
>
> But when I tried to compile, the compiler gives me an error:
>
> Game.java:31: non-static method stopTimer() cannot be referenced from a
> static context
> GamePanel.stopTimer();
> ^
> 1 error
>

The problem seems to be that you don't fully understand what static
means. A static method or a static variable is one which is common to
all members of the class, and not associated with a particular instance.

For example thinking of cars, you might have a class Car. Now it might
well have several properties associated with individual instances of a
car, such as engineRunning, doorsOpen, throttlePosition etc. and
instance methods like startEngine(), stopEngine(), slamDoor().

It will also have "static" properties such as totalNumberBuilt.
Particular subclasses such as FordEscort will properties such as width,
recommendedPrice, and methods like listAvailableColours().

So it makes sense to call FordEscort.listAvailableColours(), even when
you don't have one (or already have a fleet of them). On the other hand,
Car.startEngine() does not make sense. Which car? You haven't said.
thatParticularCar.startEngine() is much better.

So back to your problem. GamePanel.stopTimer() doesn't make sense. Which
GamePanel? Maybe you haven't even created one at all yet. If you have,
then you need to specify, like this:

     GamePanel myGamePanel = new GamePanel(); // make one
     ...
     myGamePanel.stopTimer(); // stop its timer

HTH
Steve



Relevant Pages

  • [Review] Bolide
    ... the race out to where it is a superbly fun, enjoyable game but doesn't ... car movement without being overly complicated. ... Before players move their car for the first time, ...
    (rec.games.board)
  • Re: NR2003 entry fee $100. How many new guys pay it?
    ... FWIW, even back to NR04, When the patch was done, the GD game worked, to the ... IMHO they spent more time racing a car at the ... to not get a BF while racing NSR is to sit in the pits, ... 1983Microsoft Windows was announced November, ...
    (rec.autos.simulators)
  • Re: Shockingly True Tales from the Game Store...
    ... but I remember thinking it was kind of hard to call it poverty ... afford to buy a new car, buy your own home, etc. ... a game for me. ... Thankfully, the new Chuck Norris ...
    (rec.games.miniatures.warhammer)
  • Re: Pink Wiggly Gherkins 27/7/07
    ... I will be playing Forza 2. ... then deciding that I don't like the handling of the car. ... started the game on the hardest difficulty setting probably. ... I normally soften the rear quite a lot. ...
    (uk.games.video.misc)
  • ATLARGE Thursday (BATR with no poker content)
    ... been putting off an alignment problem in my car that had been trashing ... MVP of the game if anyone was curious. ... drinks, but I'd probably card her too. ... This Gas Nazi yells at me and says ...
    (rec.gambling.poker)