How can I get class object value if given a variable in the form of a string

Viewed 133

I have a class

@lombok
class a {
private String status;
}

and I have a method that accepts a string value

public string getValue (String input, Class a) {
//  Let's say input value is status
   return a.getStatus();
}

How can I return a.getStatus()?

I'm not able to figure out a way to map these values with and without using reflection.

I can change status to getStatus as well in the input if it helps.

2 Answers

What is the input parameter in the getValue function used for? Its not doing anything in your code. If you simply want to get the value of the class objects variable, you can make that variable static and access it that way.

Use class as following :

@Data
class A {
private String status;
}

The method to return string value :

public String getValue(String input) {
    A a = new A();
    a.setStatus(input);
    return a.getStatus();
}
Related