Search code examples
javamethod-parameters

Getting passed parameter name in java?


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

Solution

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