find out instantiating object in the constructor of a class

后端 未结 3 983
花落未央
花落未央 2020-12-22 09:56

How can i get hold of the instantiating object from a constructor in java?

I want to store reference to the parent object for some GUI classes to simulate event bubb

3条回答
  •  情歌与酒
    2020-12-22 10:41

    If you want to know the invoking class, then pass "this" as a parameter to the constructor.

    Thing thing = new Thing(this);
    

    Edit: A modern IDE allowing refactoring will make this very easy to do.

提交回复
热议问题