getDeclaredField(String) vs getMethod(String) for private fields in Bean


Vivin Paliath:

I have a bean whose properties I want to access via reflection. I receive property name in string form. Beans have getter methods for their private fields.

I'm currently using getDeclaredField(fieldName), using to access the field and setAccessible(true)then using to get its value get.

Another way to solve the problem is to capitalize the field name and add it getto the beginning of the field , then get a method from the class with that name, and finally call that method to get the value of the private field.

Which method is better?

edit

Maybe I should explain what I mean by "better"... By "better" I mean best practice. Or if there are any subtle caveats or differences.

Peter Tillemans:

It depends on your use, but in general I prefer to use the getter because it's the "normal" way and in more cases it will do what the developer of the class expects to be done.

In principle, if the developer of the class makes the field private, he is free to do what he wants, for example, to remove it later (if it can be calculated in some other way). Then, if you are unfortunate enough to have no one remembered after 3 months, the site visit will be interrupted, hopefully immediately.

Note that there is a library like apache commons BeanUtils (I believe there is also a library in Spring) that does this for you and provides more sensible interfaces such as hash maps.

Related


Private fields vs private static fields in abstract classes

Asinkxcoswt I am confused about the scope of privatefields and fields of private staticabstract classes . For example, consider the following categories and pay attention to the fields validator, abstract class ValidComponent { private static Validator val

Private fields vs private static fields in abstract classes

Asinkxcoswt I am confused about the scope of privatefields and fields of private staticabstract classes . For example, consider the following categories and pay attention to the fields validator, abstract class ValidComponent { private static Validator val

How to use JavaScript private fields in VS Code?

Matt Biner I've used the suggested private field syntax in JavaScript , but VS Code says it's a syntax error: The specific error is:Invalid character. ts(1127) How to make VS Code aware of private fields? Matt Biner reason VS Code uses TypeScript projects to p

Defining Private Fields and Property vs. Property

Jose Lopez Garcia Following OOP best practices, it is best to use the following code: class Car { private Driv driver; public Driv Driver { get { return driver; } set { driver = value; } } } Or this? class Car { public Dri

How to use JavaScript private fields in VS Code?

Matt Biner I've used the suggested private field syntax in JavaScript , but VS Code says it's a syntax error: The specific error is:Invalid character. ts(1127) How to make VS Code aware of private fields? Matt Biner reason VS Code uses TypeScript projects to p

How to use JavaScript private fields in VS Code?

Matt Biner I've used the suggested private field syntax in JavaScript , but VS Code says it's a syntax error: The specific error is:Invalid character. ts(1127) How to make VS Code aware of private fields? Matt Biner reason VS Code uses TypeScript projects to p

How to use JavaScript private fields in VS Code?

Matt Biner I've used the suggested private field syntax in JavaScript , but VS Code says it's a syntax error: The specific error is:Invalid character. ts(1127) How to make VS Code aware of private fields? Matt Biner reason VS Code uses TypeScript projects to p

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

Benstpierre : I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wi

Why does JSF apply Bean Validation for shadowed private fields?

Davis I've encountered some surprising behavior with Hibernate Validator and JSF. I wonder if the behavior is a bug or a misunderstanding of my own expectations. I have this Facelets page: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//

Why does JSF apply Bean Validation for shadowed private fields?

Davis I've encountered some surprising behavior with Hibernate Validator and JSF. I wonder if the behavior is a bug or a misunderstanding of my own expectations. I have this Facelets page: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//

Why does JSF apply Bean Validation for shadowed private fields?

Davis I've encountered some surprising behavior with Hibernate Validator and JSF. I wonder if the behavior is a bug or a misunderstanding in my own expectations. I have this Facelets page: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//

Why does JSF apply Bean Validation for shadowed private fields?

Davis I've encountered some surprising behavior with Hibernate Validator and JSF. I wonder if the behavior is a bug or a misunderstanding in my own expectations. I have this Facelets page: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//

Confusion between when to use private vs protected fields

Desire PRG I've seen users in SO saying that protecting fields is bad because it can cause problems as the code grows. Please refer to the code below. public class Car { private String modelName; private int yearReleased; //getters and setters } If

private final vs public final fields in immutable objects (java)

attaboy182 Item 15 in Effective Java states that it is recommended to use private final fields instead of public final fields in immutable objects, as it may prevent changes to the internal representation in later versions . I'm having trouble understanding th

Confusion between when to use private vs protected fields

Desire PRG I've seen users in SO saying that protecting fields is bad because it can cause problems as the code grows. Please refer to the code below. public class Car { private String modelName; private int yearReleased; //getters and setters } If

Confusion between when to use private vs protected fields

Desire PRG I've seen users in SO saying that protecting fields is bad because it can cause problems as the code grows. Please refer to the code below. public class Car { private String modelName; private int yearReleased; //getters and setters } If

private final vs public final fields in immutable objects (java)

attaboy182 Item 15 in Effective Java states that it is recommended to use private final fields instead of public final fields in immutable objects, as it may prevent changes to the internal representation in later versions . I'm having trouble understanding th

Confusion between when to use private vs protected fields

Desire PRG I've seen users in SO saying that protecting fields is bad because it can cause problems as the code grows. Please refer to the code below. public class Car { private String modelName; private int yearReleased; //getters and setters } If

Confusion between when to use private vs protected fields

Desire PRG I've seen users in SO saying that protecting fields is bad because it can cause problems as the code grows. Please refer to the code below. public class Car { private String modelName; private int yearReleased; //getters and setters } If

private [this] vs private

solo In Scala I see features like object private variables. From my not so rich Java background, I learned to turn everything off (make it private) and turn it on when necessary (provide an accessor). Scala introduces stricter access modifiers. Should I always

private [this] vs private

solo In Scala I see features like object private variables. From my not so rich Java background, I learned to turn everything off (make it private) and turn it on when necessary (provide an accessor). Scala introduces stricter access modifiers. Should I always

private [this] vs private

solo In Scala I see features like object private variables. From my not so rich Java background, I learned to turn everything off (make it private) and turn it on when necessary (provide an accessor). Scala introduces stricter access modifiers. Should I always