1

This question is different from Getting the name of the current executing method.Because i want to get variable names not executing method name.

 public static int addTwoNumbers(int a,int b){
     int result=0;

     result=a+b;

     //system.out.println(error variable name);

     return result;
    }

In above code snippet addTwoNumbers function add given two values and return the result.My requirement is something like this.Let say user accidentally pass different data type values (double and string).Then i want to output that caused variable name.so to do that i want to get current executing variable name.I would like to know that is it possible to get current executing variable name via java reflection or any other API.Please share your ideas.

Community
  • 1
  • 1
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • Possible duplicate of [Getting the name of the current executing method](http://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method) – resueman Nov 10 '15 at 05:51
  • 6
    *"I would like to know that is it possible to get current executing variable name via java reflection or any other API"* - No, that's not how reflection works. *"Let say user accidentally pass different data type values (double and string)"* - That's not possible, the formal parameters prevent this from occurring. It would cause a compile time error if they tried or an `IllegalArgumentException` if they used reflection, in which case the diagnostics would be troublesome as you'd need to compare the `parameterTypes` with the types passed to the `invoke` method – MadProgrammer Nov 10 '15 at 05:51
  • 2
    Local variables are not accessible via reflection. You can call methods(constructors) and read/write fields. Even if it was (and even if passing wrong arguments to a variable is possible): The code is much more complex than `System.out.println("result");" – CoronA Nov 10 '15 at 06:06

0 Answers0