Alex Rivera | Logout

Passing "this" in java constructor

Asked 2010-03-10T18:17:05.693
17

Look into the following code:

public class ClassA {
    private boolean ClassAattr = false;

    public ClassA() {    
        ClassAHandler handler = new ClassAHandler(this);
    }
}

public class ClassAHandler extends GeneralHandler {
    ClassA ca = null;

    public ClassAHandler(ClassA classa) {
        this.ca = classa;
    }
}

I need to access ClassAattr on some ClassAHandler methods, among other attributes. Is there a way to do so without passing the origin class in the handler constructor. I don't really like how this solution "looks".

Edit
Report

1 Answer

3

Create a registerHandler(ClassA handler) method.

There is no way to create a handler for something the handler doesn't know about.

answered 2010-03-10T18:21:13.177

Your Answer