1

I want to get the value a class attribute, But I am getting exception : java.lang.NoSuchFieldException

Person.class

public class Person { public static final String name = "person name"; } 

MainActivity.class

... private void method() { Class myClass = Person.class; String name = myClass.getField("name"); } ... 

I am getting a java.lang.NoSuchFieldException exception for the getField method.

I tried these solutions but with no avail ...

  1. Change getField method to getDeclaredField

  2. Surround the code by try/catch, and got another error (Incompatible types : java.lang.String and java.lang.reflect.Field)

  3. Invalidate Android Studio caches and restart

I don't Know how to access this value, Any solutions or suggestions are welcomed.

Thanks in advance.

5 Answers 5

1

Change getField method to getDeclaredField

Surround the code by try/catch, and got another error (Incompatible types : java.lang.String and java.lang.reflect.Field)

that because getDeclaredField will return object of type Field not String, just change your code to this

Field field = myClass.getDeclaredField("name"); //do something with field 
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to access the value of the field, you can use the get(...) method with a null argument - since it's a static field, it does not require any instance:

private void method() { Class myClass = Person.class; Field field = myClass.getField("name"); String name = field.get(null); Log.d("Test", "field value: " + name); } 

In your case, it doesn't matter whether you use getField(...) or getDeclaredField(...). You would want to use the latter if you want to grab a field in its superclass or an interface implemented by your class.

For example, if Person were to extend from a class that has a field named sample, you would need to use getDeclaredField("sample") instead.

Comments

1
  1. If your variable in the class "Person" is static:
    (This is not the best solution in my opinion)

    Explanation: getField method returns a type "field" so you CAN NOT save into a variable from another type without a conversion.
    YourField.get returns an object so you CAN NOT save into a variable from another type without a conversion.

try{ Class _person = Person.class; Field field = _person.getField("name"); Object value = field.get(null); String valueString = (String)value; /*The String you are looking for*/ }catch (Exception e) { //TODO handle exception } 

  1. If your variable in the class "Person" is static:

 String valueString = Person.name /*The value you are looking for*/ 

  1. If your variable isn't static but public:
    IMPORTANT (If you have not set a default value to the variable):
    In this case the value will be an empty string because you are creating a new instance of your calss. You can set the "person name" in the constructor of your Person class another way you will get an empty string because the variable isn't static.

 Person _person = new Person(); String personName = _person.name; 

Comments

0

Since that's a constant you declared, access it directly with the class name as below,

String name = Person.name; 

Comments

0

It's a static constant. Static means there is only one value at a time possible. Or say it like this: The class attribute 'name' is a class attribute, not an object attribute! The attribute belongs to the class!

So you don't need to create an instance of your Person class.

You just can use:

String name = Person.name; 

Remember: this only works cause the name belongs to the class. And it does so, because you declared your name variable static.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.