Because as soon as you do something like
public float returnSomething(String theThing) { switch (theThing) { case "height": return height; case "rotation" : return rotation; case "width": return width; default: return 0; } }
I can feel the following question for: "Why is my height always 0?"
And then a postcode like
public class GameThingy {
Technically, the moment you make a typo anywhere, you will have trouble finding the source of the problem. This, and you are lucky that all fields are float , they do not have to be.
Edit:. By the way, this is actually a typical problem in determining the field of interest in some database queries. As if necessary, specify the field you are looking for with String .
Real example of RealmQuery<T> .
A RealmQuery<T> as follows:
RealmQuery<User> query = realm.where(User.class); // Add query conditions: query.equalTo("name", "John"); // Execute the query: RealmResults<User> result1 = query.findAll();
Where it is assumed that the User class looks something like this:
public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
It is interesting to note that Realm creates a “proxy subclass” where they override the setName and getName (aka, you need getters / setters to make some systems work!) Suppose you have them!)
But the important thing is that you need to specify the name of the field "name" .
Everyone can make a typo later, or you just won’t remember the margins from your head. In this particular case, they tend to create a metamodel (that stores the field name for you) with the goal that you do not have to use strings to indicate field names. p>
For example, in the criteria API, instead of
Root<Pet> pet = cq.from(Pet.class); cq.select(pet.get("name"));
They have a metamodel:
Root<Pet> pet = cq.from(Pet.class); cq.select(pet.get(Pet_.name));
Thus, it eliminates the need for String s.
And similarly to what I usually do with Realm, I create a “metamodel” (although now you can automatically generate it using RealmFieldNamesHelper ):
public class User extends RealmObject { @PrimaryKey private long id; private String name; public static enum Fields {
And so you can replace the request in this way
RealmResults<User> result2 = realm.where(User.class) .equalTo(User.Fields.NAME.getField(), "John") .or() .equalTo(User.Fields.NAME.getField(), "Peter") .findAll();
But with getters and setters, you already have type safety, and you already have methods that you don't need to memorize from head to head - and so you have compile-time error checking.
So, to answer your question, the reason why it is bad practice to refer to variables by string name in Java is because
1.) typos may occur and the error only occurs at runtime, not compile time
2.) it is not type safe; you are lucky that you will get a float back no matter what is in this example, but at the moment when it can be a String , you need to return an Object and apply it or use public <T> T get(String field) { ... } , and whoever calls the method needs to know exactly what they get, is also prone to runtime errors.