Possible Duplicate:
Why use getters and setters?
Is there any advantage to making methods to access private variables in your class instead of making the variable public?
For example is the second case better than the first?
//Case 1 public class Shoe{ public int size; } //Case 2 public class Shoe{ private int size; public int getSize(){ return size; } public void setSize(int sz){ size = sz; } }
Getters and setters are poor design as well. They are better than public variables because they allow the class to enforce invariants.
1) getter and setter method gives you centralized control on how a the particular field is initialized and provided to the client which makes the validation and debugging much easier. you can simply put breakpoints or print statement to see which thread are accessing and what values are going out.
The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement. 2.
Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.
What I have seen someday on SO, as answer (written by @ChssPly76) why to use getters and setters
Because 2 weeks (months, years) from now when you realize that your setter needs to do more than just set the value, you'll also realize that the property has been used directly in 238 other classes :-)
there are much more advantages:
before:
private boolean alive = true; public boolean isAlive() { return alive; } public void setAlive(boolean alive) { this.alive = alive; }
after:
private int hp; // change! public boolean isAlive() { return hp > 0; } // old signature //method looks the same, no change in client code public void setAlive(boolean alive) { this.hp = alive ? 100 : 0; }
EDIT: one additional new advange when you are using Eclipse - you can create watchpoint on field, but if you have setter you need just a breakpoint, and... breakpoints (e.g. in setter method) can be conditional, watchpoints (on field) cannot. So if you want to stop your debugger only if x=10
you can do it only with breakpoint inside setter.
Using public variable can cause setting wrong values to the variable as the input value cannot be checked.
eg:
public class A{ public int x; // Value can be directly assigned to x without checking. }
Using setter can be used to set the variable with checking the input. Keeping the instance varibale private, and getter and setter public is a form of Encapsulation getter and setter is also compatible with Java Beans standard,
getter and setter also helps in implementing polymorphism concept
eg:
public class A{ private int x; // public void setX(int x){ if (x>0){ // Checking of Value this.x = x; } else{ System.out.println("Input invalid"); } } public int getX(){ return this.x; }
Polymorphic example: We can assign Object Refernce Variable of the Sub type as Argument from Calling method to the Object Refernce Variable of Super Class Parameter of the Called method.
public class Animal{ public void setSound(Animal a) { if (a instanceof Dog) { // Checking animal type System.out.println("Bark"); } else if (a instanceof Cat) { // Checking animal type System.out.println("Meowww"); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With