Re: Checking objects static or not
From: Steve Horsley (shoot_at_the.moon)
Date: 11/20/03
- Next message: philoso: "HELP. newbie <html:form action...> acting funny."
- Previous message: lasitha: "Re: using ANT for regular nightly invocation of JUnit"
- In reply to: fix: "Checking objects static or not"
- Next in thread: fix: "Re: Checking objects static or not"
- Reply: fix: "Re: Checking objects static or not"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: philoso: "HELP. newbie <html:form action...> acting funny."
- Previous message: lasitha: "Re: using ANT for regular nightly invocation of JUnit"
- In reply to: fix: "Checking objects static or not"
- Next in thread: fix: "Re: Checking objects static or not"
- Reply: fix: "Re: Checking objects static or not"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|