Reputation: 19036
In my all JSF
projects i use the standard definition of Pojo
As a Plain Old Java Object
which define private
modifier for fields
For Example
public class Pojo {
// My private fields
private String field1;
private String field2;
// setters AND getters
// ----------------------------
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
// ----------------------------
}
Is there is any risk to use public
modifier instead of private
with my Pojo
fields
For Example
public class Pojo {
// My private fields
public String field1;
public String field2;
// setters AND getters
// ----------------------------
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
// ----------------------------
}
Note
I really need to do that (change modifier) not just for experiment
Upvotes: 1
Views: 1149
Reputation: 1503090
The "risk" is that you basically no longer have any control over the values. You can never enforce any kind of validation, because any code can change the value at any time.
Also, you can never change the implementation detail of how the data is stored, because you're making that public. With just the getters and setters, you've separated the API (what the class can do) from the implementation details.
Now you may decide that that's all fine (although I wouldn't). If you do go down the public field route, I would advise you to remove the getter/setter methods - otherwise anyone seeing the methods might look like you've got a "proper" class when really you've just got a data bucket.
Upvotes: 11