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"); } 
3
  • 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. Commented Aug 17, 2011 at 6:49
  • 2
    (this post was closed as a duplicate of What is the point of setters and getters in java?). Commented Aug 17, 2011 at 6: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. Commented Aug 17, 2011 at 6:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Now if Java really helped with Encapsulation and let Getters/Setters be indistinguishable from normal "property access"... :-/
Yep. Scala for instance obeys the uniform access principle quite nicely :-)
Unfortunately switching the implementation from direct to getter/setter swill still break binary compatibility :( Also an issue with .NET though...
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.

1 Comment

Setters doesn't expose the field.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.