0

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 :)

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by javaAddict
0

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().

1

(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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.