0

Possible Duplicate:
Are Getters and Setters evil?

I can't find a logical reason behind having a private variable with a getter and a setter that does nothing but directly handling the value being preferable to having a public variable.

Am I missing something?

Community
  • 1
  • 1
Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • 2
    [Here](http://stackoverflow.com/questions/565095/are-getters-and-setters-evil) is one of many many posts on this on SO. – Keppil Jul 25 '12 at 06:38

7 Answers7

5

Because,

Validation is one reason. Keeping the field name out of the public API also allows you to change it later without breaking the API. And it allows you to change the class later in other ways as well, e.g. moving the field to some other class (so that the public setter would call a setter in a different class). Having the setter called also allows you to do other things, e.g. notify interested other components of the change of value. None of this would be possible if the field was accessed directly.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • There are also OOPs design patterns like Encapsulation, Type-Conversion all are matters. If you read the basic OOPs design concepts its nice to understand how Object Oriented Programming structure works.. – user370305 Jul 25 '12 at 06:45
2

They are preferred to future proof the code. In the future if you want to eliminate the variable or use another variable to derive this variables value - the change is simpler. You just need to change the getter/setter, the rest of the code remains unaffected. This is not the case with direct access to the variable.

ThirdOne
  • 1,227
  • 9
  • 13
1

As @user370305 already mentioned one reason is validation. Other reason is types conversion. Setter may accept string and parse it to integer. Yet another reason is data encapsulation. It is not necessarily to have a simple filed stored in the same class. Method setName(String) of class Person may store the name in more complicated data structure. Using simple field does not allow you to change the internal implementation of class Person without affecting code that uses it.

EDIT: yet another technical reason. It is much easier to discover and debug code with getters and setters. If some field is changed unexpectedly you can just toggle break point into appropriate setter and find the problem very quickly. If this field is public and you have 1000 references to this field you theoretically have to put 1000 breakpoints in all these places.

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

1. Encapsulation has different use in different context, In design patterns its like behaviors that keeps changing needs to be encapsulated in abstract class, or interface.

2. Having private instance variable and public getter setter is b

3. Its mainly done to Validate the input from the user... Setting the value to an instance variable directly is dangerous.

Eg:

int dogAge;

       System.out.println("My dogs age is :"+dogAge);

Now what if someone gives a negative age... then.......???

So we must do it this way...

int dogAge;

      public void setAge(int age){

       if (age>0){
         dogAge = age;
        }
       else{

              System.out.println("Not a valid age");
           }

     }

     public int getAge(){


        return dogAge;

      }

    System.out.println("My dog age is :"+ getAge());
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Its simple .. if you make those variable public then you give rights for ading any values to them . But if you do that via getter or setter ... you can put checks over it and control the input or conversion without letting the end user know that

eg :

getName(){
  return firstName+lastName;
}

or

 getData(){
  // code to convert byte to Mb or whatever you like to represent
 }
ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
0

Use of accessors to restrict direct access to field variable is preferred over the use of public fields, however, making getters and setter for each and every field is overkill and considerd as not a good practice. It also depends on the situation though, sometimes you just want a dumb data object. Accessors should be added for field where they're really required. See this link to know more about it Getter Setter: To Use or Not to Use.

BBdev
  • 4,898
  • 2
  • 31
  • 45
-1

I can't find a logical reason behind having a private variable with a getter and a setter that does nothing but directly handling the value being preferable to having a public variable.

Consider that any additional code that you put into getters and setters adds to complexity and also needs to be tested. For a small system which is fully controlled by you, there may be little benefit in using getters and setters. Use your professional judgement. You may not need the future proofing and added complexity. Or it may be more important to you to have the efficiency and simplicity of direct access.

Personally, I think that getters and setters are over-used. For a small system which is fully controlled by you, direct access may be the way to go.

2.718
  • 435
  • 2
  • 9