5 Replies Latest reply on Sep 2, 2010 1:36 PM by 843807

    JavaBean - violation of encapsulation

      The principle of JavaBeans ara use of getter/setter methods

      public class Employer {
           private String name;
           private String address;
           private String city;
           private String state;
           public String getName() {
                return name;
           public void setName(String name) {
                this.name = name;
           public String getAddress() {
                return address;
           public void setAddress(String address) {
                this.address = address;
           public String getCity() {
                return city;
           public void setCity(String city) {
                this.city = city;
      The famous java framework as Spring active use JavaBeans.
      But when we use getter/setter methods we violation of encapsulation.
      I found the article of Allen Holub - "Why getter and setter methods are evil"

      I fully agree with this. article.
      What are you think about this?
        • 1. Re: JavaBean - violation of encapsulation
          I would just say that it is completely irrelevant to JavaBeans. Getters and setters are part of the contract of a JavaBean. No way round it, and fourteen years too late to start the discussion.
          • 2. Re: JavaBean - violation of encapsulation
            OK, do you agree that getter/setter methods are violation of encapsulation?
            • 3. Re: JavaBean - violation of encapsulation
              excerpt from the article "More on getters and setters" by Allan Holub:

              "Unfortunately, the getter/setter idiom that many programmers think of as object oriented violates this fundamental OO principle in spades. Consider the example of a Money class that has a getValue() method on it that returns the "value" in dollars. You will have code like the following all over your program:

              double orderTotal;
              Money amount = ...;
              orderTotal += amount.getValue(); // orderTotal must be in dollars

              The problem with this approach is that the foregoing code makes a big assumption about how the Money class is implemented (that the "value" is stored in a double). Code that makes implementation assumptions breaks when the implementation changes. If, for example, you need to internationalize your application to support currencies other than dollars, then getValue() returns nothing meaningful. You could add a getCurrency(), but that would make all the code surrounding the getValue() call much more complicated, especially if you persist in using the getter/setter strategy to get the information you need to do the work. A typical (flawed) implementation might look like this:

              Money amount = ...;
              value = amount.getValue();
              currency = amount.getCurrency();
              conversion = CurrencyTable.getConversionFactor( currency, USDOLLARS );
              total += value * conversion;

              This change is too complicated to be handled by automated refactoring. *Moreover, you would have to make these sorts of changes everywhere in your code*."

              What I you think about this?
              • 4. Re: JavaBean - violation of encapsulation
                Yes I read it. By and large I agree but it's rather overstated especially in the context of JavaBeans which was already a done deal at the time. Many application architectures have whole layers of objects which have no behaviour at all and no other purpose but to carry state.
                • 5. Re: JavaBean - violation of encapsulation
                  Hi !!!!

                  Well, I think getter/setter are useful but you shoudn't use these methods always just when you need to, I mean: don't code getter/setter foreach atribute you have, if you need to retrieve an atribute use a getter and if you need to assign a value to an atribute use a setter. It depends of your app's context.