Should all the setter methods in the JavaBeans remain non-public?

A public setter method in a public class is equivalent to a public member variable in a public class. For example,

public class MyBean

{

private int myInt;

MyBean() {}

public int getMyInt() {

return myInt;

}

public void setMyInt(int aint) {

myInt = aint;

}

}

With the above implementation, a private member variable (myInt) can be reset to be any value since we have a public method in a public class. Should we restrict all the setter methods to be non-public?

[542 byte] By [khwang1044a] at [2007-11-27 11:07:19]
# 1

No. Either have them public or don't have them at all, f you want a property to be read-only

You've inadvertently touched on a slightly controversial subject, actually: the matter of whether getters and setters violate encapsulation. But I won't go into that right now :-)

georgemca at 2007-7-29 13:21:26 > top of Java-index,Desktop,Developing for the Desktop...