Take the 2-minute tour ×

My constructor looks like this:

public MainApplet() {
   super(new TitansArena(), false);

I want to be able to pass a reference of my MainApplet object to TitansArena constructor, how can I do that?

share|improve this question
Yes, but that will not be compiled by java :D –  Romeo Jun 15 '13 at 23:29
Do not do that in the MainApplet constructor, it is bad practice (and has a name, constructor leakage) –  SJuan76 Jun 15 '13 at 23:29
you mean the mainapplet reference to titansarea or another reference to mainapplet and to titansarea? –  terrybozzio Jun 15 '13 at 23:31
I want to access MainApplet from inside TitansArena. –  Romeo Jun 15 '13 at 23:32
Let's take a step back here. What technical problem are you having that you think asking this question will solve? That is, why are you doing this in the first place? This is generally a very bad idea. And I see no good reasons for why your classes should need to hold a reference to Applet or your derivatives of it. –  Jason Jun 15 '13 at 23:39
show 10 more comments

1 Answer

up vote 0 down vote accepted

I've managed to find a way 'around' the problem like this:

public class MainApplet extends LwjglApplet {
    private static final long serialVersionUID = 1L;
    TitansArena game;
    public MainApplet() {
        this(new TitansArena());

    private MainApplet(TitansArena ta) {
        super(ta, false);
        game = ta;
        game.ap = this; 

Where ap is declared like this: public Applet ap; in TitansArena.

share|improve this answer
this is creepy, why you want you store in an atribute field a reference of itself, and why if you pass super(ta,false) then you game = ta ; no sense.. –  nachokk Jun 16 '13 at 1:19
I need the to create a JSObject window = JSObject.getWindow(); Inside TitansArena class. In order to do this i need a reference to applet so this is why I keep a reference to my applet in game.ta . It actually has a sense. –  Romeo Jun 16 '13 at 9:50
Really not .. it's circular.. to obtain your TitansArea is inside your applet class .. think about it –  nachokk Jun 16 '13 at 14:52
SO how can you create a JSObject inside TitansArena? The constructor needs an Applet argument. –  Romeo Jun 16 '13 at 15:05
add comment

Your Answer


