0

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?

MagnusCaligo
  • 707
  • 2
  • 8
  • 28
  • 2
    Why do you feel the need to do this? Unless you pass in the name of the class yourself or use AOP, it's unlikely. If you could explain what you're trying to use it for, that may help pin down what you need to do. – Makoto Oct 27 '14 at 01:09
  • Well, i'm in AP Physics in school and I'm working on a 2D physics engine because why not? And one of the main difficulties I am having is how to determine if an object is on an incline plane or not. Because if the object is on an incline plane it requires a different set of equations then when it isn't. The only way I can see doing this is by giving each object a "Type" and then checking to see if that object at that point is on an inclines plane or not, that way I know which equations to use... So the point of this is that if a force is initiated on an object, this method will be called – MagnusCaligo Oct 27 '14 at 01:12
  • to give the inverse of that force, which will then require a different "From" because the force might be from either an inclined object or not. Sorry if that sounds confusing, it made sense in my head. – MagnusCaligo Oct 27 '14 at 01:13
  • *"...it requires a different set of equations then when it isn't. ...the force from either an inclined object or not..."* These sound like usual OOP scenarios that are dealt with by very traditional techniques like polymorphism, structural composition, etc. That's if you don't want to approach with logic, e.g., `if(isFromInclinePlane) [ calcForInclinePlane ] else [ calcOtherwise ]`. – Radiodef Oct 27 '14 at 01:26
  • Yea, the problem is telling if it is on an inclined plane, that is why I have to do this... I need to give the object a "type" and say that the object is a line at 30 degrees, then I have to use different set of equations, so when a ball hits the line a force is exerted on the line FROM the ball, but there is also a force exerted on the ball FROM the line, which both have different equations, so I need to find a way to determine FROM what the force is coming from. – MagnusCaligo Oct 27 '14 at 01:38

1 Answers1

1

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.

Community
  • 1
  • 1
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156