Is there a way in Java to find the name of the variable passed to the function?
•
Java
I have a Java function called testfornull
public static void testForNull(Object obj) { if (obj == null) { System.out.println("Object is null"); } }
I use it to test multiple objects to make sure they are not empty However, I cannot tell the name of the variable in this way
For example, if I say
testForNull(x); testForNull(y); testForNull(z);
I don't know which of the three lines causes the output of "object is null" Of course, I can simply add another parameter to the function and have something similar
testForNull(x,"x"); testForNull(y,"y"); testForNull(z,"z");
But I wonder if I can infer the name of the variable instead of passing it explicitly thank you.
Solution
Consider that the parameter may not be a variable (so it will not have a name):
testForNull(x != y);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码