In Java, how to get attribute given the string with its name?

前端 未结 5 1062
粉色の甜心
粉色の甜心 2020-12-25 13:51

I\'m sorry for asking this sort of questions, but I really couldn\'t find the answer in Google. So say I have a class with private String myColor and I have a s

5条回答
  •  甜味超标
    2020-12-25 14:36

    It depends where you want to do this. Inside the class you simply whatever with it, e.g:

    myColor = "blah blah";
    

    From outside, you need to have some public method generally as other posts indicated. In all cases, you have to be careful if your environment in multi-threaded. Class level variables are not thread safe.

提交回复
热议问题