0

Following example

public class C{
    A myA;

    public C(){
        myA = new A();
    }
}

public class A{
    C myOrigin;

    public A(){
         // How to set myOrigin to the instance 
         // which invoke this.
    }
}

Is there any way, to get the instance of class C, which creates the instance of A (inside this instance). With other words: Does an instance of A know the object from where it was initialised.

I know, I can use this as an parameter,

public class C{
    A myA;

    public C(){
        myA = new A(this);
    }
}


public class A{
    C myOrigin;

    public A(Object pObject){
         myOrigin = pObject;
    }
}

but I'm searching for a way without any parameter.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ma Sch
  • 501
  • 1
  • 4
  • 8
  • 3
    Can you elaborate why you need this in the first place? This seems like a very unusual request as it goes against the principle of encapsulation where as much information as possible is hidden and only what is needed is exposed. – Konrad Höffner May 12 '22 at 13:29
  • 1
    The reason this is hard to do is because you really, really should not be doing it. No one should do it. Class A should function in exactly the same manner for every caller. – VGR May 12 '22 at 23:49

1 Answers1

0

I very much doubt it's possible, but would be interested to hear otherwise. The reason I doubt it is because it's not an instance that calls a method, but another method. The calling method can be found easily through the stack trace (e.g. throw & catch an exception and examine its stack trace to find the caller). However, the instance is just a hidden argument to the method, and we have no way of knowing what arguments the calling method was called with. (At least, not to my knowledge.)

So, I think passing this to the other method is the only way.

k314159
  • 5,051
  • 10
  • 32
  • I didn’t do any testing but StackFrame allows to retrieve local variables: https://docs.oracle.com/javase/8/docs/jdk/api/jpda/jdi/com/sun/jdi/StackFrame.html#getValue-com.sun.jdi.LocalVariable- And since this is technically just a local variable in the parent StackFrame that might work – Felix May 12 '22 at 13:39
  • That's an interesting thought. It might work; however, that's part of the Java Debug Interface, which is part of the JDK, not the JRE. The JDK may not be available where the application is running. – k314159 May 12 '22 at 13:50
  • Yes, also it’s only available for suspended Threads which makes it useless for OPs case (a suspended thread can’t do the lookup); I meant to look for StackWalker.StackFrame and didn’t notice I ended up on a different page – Felix May 12 '22 at 14:20
  • 2
    @Felix it is possible when you use another thread for helping. I did this in [this answer](https://stackoverflow.com/a/62468197/2711488). But of course, using a debugging feature should not be used as part of the application design. Further, there is no guaranty that `A` has been instantiated from a `C` instance at all. The only reasonable scenario is if `A` is an inner class of `C`, that would also solve the problem, but doesn’t seem to be what the OP had in mind. – Holger May 12 '22 at 14:27