0

I have a class which implements ActionListener. My actionPerformed() method runs fine. However, there are some things I want to do once before my program starts to run actionPerformed(). I could put a boolean in actionPerformed() and just run it once that way, but I was looking for something cleaner. I cannot just use my constructor, because I need a fully constructed object for what I am trying to do. Is there a way to add a method such as initialize() or start() which will run before actionPerformed() starts?

Some sample code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game implements ActionListener {

    public Game() {
        // Constructor
    }

    public void initialize() {
        // I want a method of some sort here which is run before the main game
        // loop so I can set it up
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // main game runs
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • `I need a fully constructed object` - then add the code at the end of the constructor. The actionPerformed() method has nothing to do with constructing an object. You need to first create the object before you can invoke its actionPerformed() method. – camickr May 24 '14 at 03:27
  • do you want initialize() to execute every time you create an object ? – Karibasappa G C May 24 '14 at 03:29
  • @camickr I cannot unfortunately. Because I need the instance of the Object itself to set up some stuff. Quite the noodle scratcher, eh? – Evorlor May 24 '14 at 03:29
  • @karibasappa G C yes. i will (probably) only be creating the object one time tho, if it is relevant – Evorlor May 24 '14 at 03:30
  • ok then see my answer below – Karibasappa G C May 24 '14 at 03:32
  • @mKorbel downvote because i didnt include the Swing tag? or perhaps u can shed some light on why you find this a poor question, please. An answer would also be appreciated – Evorlor May 26 '14 at 21:27
  • @Evorlor by default I'm always notify about reason(s), this is important for OP, true is that I'm suprised that this empty question isn't closed as of topics, e.i., look into my profile for more info about my voting score – mKorbel May 26 '14 at 21:41
  • @mKorbel hmm...if this is off topic and only relevant to OP, i suppose i need to reread the definition of a good question. – Evorlor May 26 '14 at 23:04

1 Answers1

0

you can use Instance initialization block(IIB) if you want initialize to happen every time we create object.

and Static Initialization block(SIB)if you want initialize to happen only once.

so in your case yo want on object creation but before building an object.

so go for IIB block like below

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game implements ActionListener {

    public Game() {
        // Constructor
    }

    {
        // I want a method of some sort here which is run before the main game
        // loop so I can set it up
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // main game runs
    }

}

Note: i removed method signature of initialize and made it IIB. which will fulfill your requirement. when you do Game g = new Game();

before your constructor Game() executes IIB block executes and you will have all set to call actionPerformed() method.

Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
  • So if i wanted to do something like a Start Menu here, I would just make a new thread with an ActionListener of its own? And then actionPerformed would not be called until this thread died? – Evorlor May 24 '14 at 03:35
  • oh so it sets the field before my object is contstructed? i need the object to be instantiated before i can mess with those fields tho – Evorlor May 24 '14 at 03:39
  • I just read up on this, and I don't think this is what I am looking for. Thanks though. Learned something new (that I will use in other areas :) ) – Evorlor May 24 '14 at 03:42
  • oh then you cant use SIB or IIB as stated above..you need to call initialize() method at the end of the constrctor – Karibasappa G C May 24 '14 at 03:42
  • correct. thanks though. I will give you an upvote after I find the answer to my question :) want to wait tho so others dont think i have it yet – Evorlor May 24 '14 at 03:43
  • ok thats not a problem..other option is call initialize at the start of actionPerformed() method...or if you know AOP in spring that will also work very nicely. – Karibasappa G C May 24 '14 at 03:46