I am instantiating an object of one class with aanother class..
here is how the line is..

Class1.method1();


where method1 goes something like this ..

method1(){
Class2 c = new Class2();
}

After creating the c object of Class2, is there anyways i can know which class instatiated it(Can i get the class name of Class1 using c)??
Thank you :)

Recommended Answers

All 2 Replies

After creating the c object of Class2, is there anyways i can know which class instatiated it

Yes, because the method that instantiates the 'c' Object is inside of Class1, therefore you already know that it was instantiated inside of Class1. Inside of method1(), the word "this" refers to the Object that called method1().

(Can i get the class name of Class1 using c)??

BestJewSinceJC's answer was not wrong.

But the answer to the above question, is no. After taking the 'c' instance you cannot programmatic-ally know in which class it was created.
Unless you make some changes.

As BestJewSinceJC said inside method1 you know that you are inside the Class1. So change the Class2 constructor that takes as argument an object. And whenever you create a 'Class2' pass as parameter the object in which Class2 was created. Or better pass the Class of that object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.