Android ART can’t set final fields correctly

January 2016

For example you have class Person. Person is a POJO for REST-service (for instance, for using with retrofit).

class User {
  public static final String firstName = "", lastName = "";
}

This line of code creates two read-only fields. It’s necessary to assign initial values because every field is final. But there’s another way to do the same thing:

class User {
  public static final String firstName, lastName;

  public User() {
    firstName = "";
    lastName = "";
  }
}

It’s hard to believe, but second – is the only right way to create class.

For example, you’re creating class instance:

User user = SomeReflection.getUser();
// user.lastName.equals("Username")
callFunction(user); // in this function user.lastName.equals("") for 1st method

I think it depends on JVM but it’s truth for Android ART.

Leave a Reply

Your email address will not be published. Required fields are marked *