0

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?

Romeo
  • 376
  • 1
  • 3
  • 14
  • 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
  • The title of this question bears no resemblance to the code contained in it. They're basically contradicting one another. – Brian Roach Jun 15 '13 at 23:34
  • Sorry, i am a little tired so my english skills are tired too, i've spent the last 4 hours trying to find a solution for this :D – Romeo Jun 15 '13 at 23:36
  • well everyone on their toes to downvote 2 sec 2 down whow,sorry romeo – terrybozzio Jun 15 '13 at 23:37
  • Nothing should ever need a reference to `MainApplet` if it's your class that extends `Applet`, really. – Brian Roach Jun 15 '13 at 23:39
  • 2
    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](http://stackoverflow.com/questions/9851813/java-leaking-this-in-constructor). 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
  • You have to create one object, then the other. You can't pass `this` until after the super line, in fact it is allowed but often a bad idea to pass it anywhere in the constructor. – Peter Lawrey Jun 15 '13 at 23:40
  • What about a JSObject.getWindow(); which i have and (needs to stay there) in TitansArena? – Romeo Jun 15 '13 at 23:40
  • ok romeo look at this post please http://stackoverflow.com/questions/4717953/passing-reference-of-class-to-another-class – terrybozzio Jun 15 '13 at 23:42
  • @Romeo: Well, since we don't know what `TitansArena` represents, we can't say for sure. What conceptually does `TitansArena` represent? – jason Jun 15 '13 at 23:44
  • I am sorry guys for wasting your time. I have found a way around it here: http://stackoverflow.com/questions/12737141/keep-reference-to-new-object-passed-into-super-constructor?rq=1 Thank you all :D – Romeo Jun 15 '13 at 23:45

1 Answers1

0

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.

Romeo
  • 376
  • 1
  • 3
  • 14
  • 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 01: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 09: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