1

How do I get a passed parameter name i.e the name with which it was passed at runtime?

For example:

int mParam = 10;

public void func (int myParam){
 //print wanted name
}

func(mParam);

Wanted output:

mParam

Using reflection, I managed to get:

myParam
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
osh
  • 81
  • 1
  • 5
  • Scroll down in the duplicate question for the Java 8 solution. – rgettman Mar 21 '16 at 17:22
  • 2
    @rgettman I don't think the OP wants a parameter name, I think they want a *field* name (that happens to get passed as a parameter). It's not entirely clear. Notice that the specified output is *not* a parameter name in their example. – Paul Boddington Mar 21 '16 at 17:23
  • @PaulBoddington Yes, it wasn't clear. But because the desired output is "mParam", not "myParam", I'll un-dup it. (Already un-duped.) – rgettman Mar 21 '16 at 17:25
  • 5
    @PaulBoddington If that's the case, I'm not sure that's possible. Even if it were, that sounds like a a very poor, hacky solution to an XY problem. – Mage Xy Mar 21 '16 at 17:26
  • @PaulBoddington I'm not sure I understood you. If I call func(foo) it should print "foo", if we call func(bar) it should print "bar" – osh Mar 21 '16 at 17:31
  • 1
    Why do you need the name? Incase you need it for changing its value or anything, pass by reference will do good to you. Let us know why you want it so we can help you with a better solution to the actual requirement – Suvarna Pattayil Mar 21 '16 at 17:31
  • Wouldn't `mParam` be evaluated as `10`, then `func(10)` is called? If so, how would you get the name? The method shouldn't know what variable was passed to it, only the value given – OneCricketeer Mar 21 '16 at 17:31
  • 2
    @osh Integers do not have names. If an integer happens to be stored in a particular variable, that fact is not conveyed by the integer itself. – Kevin Krumwiede Mar 21 '16 at 17:32
  • @osh What should `func(foo + bar)` print? The method doesn't make sense. – Paul Boddington Mar 21 '16 at 17:34
  • this is not a variable `mParam` that is passed to `func()`, only its value. Inside `func()` it is impossible to discover which particular variable it initially belonged to. – Alex Salauyou Mar 21 '16 at 17:37
  • You cannot do this in Java. – BPS Mar 21 '16 at 17:42
  • Which output would you expect on `func(42);`? Wanted to say you are asking for an information, that isn't there. Calls on methods transport parameters, no parameter meta information. – blafasel Mar 21 '16 at 17:53
  • Thanks everyone. I understand my mistake now – osh Mar 22 '16 at 09:19

2 Answers2

0

I think It is not possible in java. Since java uses pass by value to pass the parameter. So when you pass the parameter to func() it only passes the value of variable nothing else.

It is possible to know about the parameter defined by function using reflect.parameter package in java.If you want to know the parameter details of the function you calling you can check out this question Can I obtain method parameter name using Java reflection?

But you can not know the name of the parameter which is passed.

Community
  • 1
  • 1
Denis
  • 1,219
  • 1
  • 10
  • 15
-1

You can change your method func to receive 2 params such as:

public void func (int myParam, String myParamName){
 //print wanted name
}

Or better yet to pass a Map as a parameter:

    public void func (Map<String, Integer> map){
 //print wanted name
}

This way you can pass named integers and get your integer by name in your method

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36