2

I am new to Java.

  1. Can any one explain what the getters and setters method do?

  2. where we must use getters setters method and how it differ from the normal method?

  3. And i saw some coding having only setter method so here why getters method not declared?

    private String wheel;
    
    /**
     * @param wheel the wheel to set
     */
    public void setWheel(String wheel) {
        this.wheel = wheel;
    }
    public void rotate() {
        System.out.println(wheel+"rotated");
    }
    
aioobe
  • 413,195
  • 112
  • 811
  • 826
BoomirajP
  • 329
  • 2
  • 7
  • 14
  • 2
    He is asking 1) **how they differ** from normal methods, and 2) Why the above code is **missing a get-method**. Neither of which is answered in the linked question. – aioobe Aug 17 '11 at 06:49
  • 2
    (this post was closed as a duplicate of [What is the point of setters and getters in java?](http://stackoverflow.com/questions/1461598/what-is-the-point-of-setters-and-getters-in-java)). – aioobe Aug 17 '11 at 06:58
  • 1
    Tim Post, as there are only 4 voters to close, I'm assuming you closed this as a moderator. Was that really the right thing to do? I can only see how the related question answer one out of the three question @BoomirajP asked here. – aioobe Aug 17 '11 at 06:59

2 Answers2

4

Can any one explain what the getters and setters method do?

Get-methods and set-methods help in encapsulating data. That's all. Instead of writing

object.wheel = new Wheel(5);
// ...
object.wheel.rotate();

you do

object.setWheel(new Wheel(5));
// ...
object.getWheel().rotate();

This gives you better control of the update of the field. You could for instance:

  • Throw an IllegalArgumentException if the wheel doesn't fit.
  • Compute or load a new wheel on the fly in the getWheel-method.
  • Let other object listen for wheel-updates

etc.

where we must use getters setters method and how it differ from the normal method?

You don't have to use getters and setters, it's just good practice.

Technically speaking getters and setters are no different from normal methods. They just have a specific (simple) purpose.

And i saw some coding having only setter method so here why getters method not declared?

The author of the class simply didn't want to expose the wheel-object to the user. The reasons for this may vary.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Now if Java *really* helped with Encapsulation and let Getters/Setters be indistinguishable from normal "property access"... :-/ –  Aug 17 '11 at 07:03
  • Yep. Scala for instance obeys the [uniform access principle](http://en.wikipedia.org/wiki/Uniform_access_principle) quite nicely :-) – aioobe Aug 17 '11 at 07:10
  • Unfortunately switching the implementation from direct to getter/setter swill still break binary compatibility :( Also an issue with .NET though... –  Aug 17 '11 at 17:09
0

Getters expose the fields. So you saw code with only setters- that makes it write only, with no read privileges. Setter => Write, Getter => Read.

These methods are typically very simple, and you can choose to handle errors in them too.

Normally you would have a private field, and either a getter or a setter (or both) to access it.

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43