Reputation: 707
Sorry if the title sounds confusing, i'll try to explain...
What I have is a class (Force) which requires a string in the constructor
public Force(double mag, double ang, String f){
magnitude = mag;
angle = ang;
from = f;
}
However later I Have a function (within the same class) that returns a new Force
public Force inverse(){
return new Force(magnitude, toRads(angle+180), //String goes here );
}
The problem is, I want the name of whatever class/object which is calling that method to be put into the parameters of new constructor. Is there any way of doing this?
Upvotes: 0
Views: 59
Reputation: 19218
I can't judge whether you should really do it this way, or manage to change your program to pass in the string. But in general, you would be able to get that by looking at the stack. This answer almost gets you there: https://stackoverflow.com/a/442789/709537
Not sure how much time it costs to create the stack trace, if your method is called very often, you should check that.
Upvotes: 1