Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Necessity of getter methods [duplicate]

Tags:

java

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);
    }
}          
like image 290
FirmView Avatar asked Aug 09 '12 12:08

FirmView


People also ask

Why do we need getter methods?

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. Given this, getters and setters are also known as accessors and mutators, respectively.

What is the benefit of getter and setter methods?

1. 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.

Why we use getters setters instead of public variables?

The main difference between making a field public vs. exposing it through getters/setters is holding control over the property. If you make a field public, it means you provide direct access to the caller. Then, the caller can do anything with your field, either knowingly or unknowingly.

What is the getter method?

What is Getir? Getir works by providing a warehouse of groceries and employing 'pickers' to get your order together quickly. It'll be delivered to you via motorbike or bicycle.


3 Answers

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.

like image 164
Jeff Foster Avatar answered Nov 16 '22 02:11

Jeff Foster


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.

like image 36
NPKR Avatar answered Nov 16 '22 02:11

NPKR


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.

like image 29
Nandkumar Tekale Avatar answered Nov 16 '22 00:11

Nandkumar Tekale