I'm wondering How can I pass field name of a class as parameter to include it in a String?
For example, let's say I have a class A with this members :
public class A {
String name;
String reference;
....
}
What I want, is to be able to initialize a string having as parameters one of the two classe field name to display it dynamically. So for instance, I want to do this :
System.out.println("the field {nameField} belong to class A.");
And in output I would have this :
output : the field name belong to class A.
Is that possible in Java ?
Any help would be much appreciated ?
Regards
YT
CodePudding user response:
This can be done by use of reflection in Java. https://www.oracle.com/technical-resources/articles/java/javareflection.html#:~:text=Reflection is a feature in,its members and display them.