'Java, get calling instance [duplicate]

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 k314159