Let’s say there is a class called Node and another class called Table. A node object can create a Table object. Obviously then the Table class’ constructor is called. Is it possible to get from within the Table constructor, a reference to the Node object which is creating this Table instance? One way is if while creating the object, I force the Node object to pass “this” as a parameter to the Table constructor. Is there any other way where no parameter needs to be passed?
Share
The only way to get a reference to the “calling” object (the instance calling a method, constructor, etc.) in the Java language is to pass the “
this” object reference to the “receiver” method which needs to know. There is no other way using plain Java, AFAIK.