9

Possible Duplicate:
Why use getters and setters?

This is a newbie question. Is it very much necessary to use getmethods to access property values? Once the value has been assigned, one can get the values directory. For example, in the below code, displayName() can display firstName value without the help of any getter method. Or it is a standard coding standards that one must have getter and setter method or any other methods which gives that value?

class Test{

    private String firstName;

    public void setName(String fname){
        firstName = fname;
    }

    public void displayName() {
        System.out.println("Your name is " + firstName);
    }
}          
Community
  • 1
  • 1
FirmView
  • 3,130
  • 8
  • 34
  • 50

6 Answers6

12

Tell, Don't Ask is an important principle in object-oriented design. Generally you should tell objects to do things rather than ask them questions. getters/setters every where discourage this practise because you are encouraged to reach inside an object and get to the fields (or even worse reach in and poke things about in the case of setters). This breaks encapsulation and makes your code harder to reason about.

In your particular case, I'd create an object called Name that has a constructor taking the name and single method to display it.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
4

In Your case (to display the display name) it is not neccessary to provide Getter.

But if your want use the field in another class We need to provide the Getter method.

NPKR
  • 5,368
  • 4
  • 31
  • 48
3

Getter and setters are a part of the standard interface for Java Beans and many frameworks like Hibernate expect them in place. That being said it is of course up to you to decide if and when you need them and for what purpose. They provide access to your private member variables and they can even give you the chance to do more than just plain get and set.

The point of OO software is reuse. This means that other programmers, or you years from now, can use the code for other systems.

When you have private member variables, and use get/set functions, you can change the internal implementation of the function without breaking all the other code that uses it.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
2

Do always use Getter and Setter to access your properties!

You should take a look at this article...

Community
  • 1
  • 1
oopbase
  • 11,157
  • 12
  • 40
  • 59
  • 3
    even in cases of package private classes and nested classes? – Eugene Aug 09 '12 at 12:36
  • 3
    Does this also apply if you want to access the field from within the class (like he is in this example)? I don't think so... The article only talks about outside access and the alternative use of public fields (which I agree should be held to a minimum). – brimborium Aug 09 '12 at 12:36
2

Having private state, encapsulation is good, and in A LOT of cases this is the right thing. Suppose that your class is suppose to be Thread Safe, having public fields you can't ensure that.

On the other hand there are cases when this is useless! Suppose that you access your object only in one package, you are sure you will never export it, then why bother?

Eugene
  • 117,005
  • 15
  • 201
  • 306
2

I do not have any links to support this, but it's what I do.

I try to avoid public fields if they are not static. So I just use protected and private fields. From within the class, you can access them without get/set, that's completely fine. From outside the class, always try to use get/set.

So your example code is completely fine to me. :)

EDIT: One exception for me is if I create a struct-like container class like this

class Point4D {
  public int x1, x2, x3, x4;
}

Then I think that public fields are ok. It would be still better to make them private and name the getters public int x1() etc though. As soon as some methods are introduced to this container that change the state of the instance (like changing the values of x1/x2/x3/x4), I make them private and add get/set.

brimborium
  • 9,362
  • 9
  • 48
  • 76