Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad practice to add elements to List using getter method in java?

Suppose I have a private ArrayList or a LinkedList inside a class, that I will never assign new reference to it, or in other words this will never happen:

myLinkedList = anotherLinkedList;

So that I won't need to use setMyLinkedList(anotherLinkedList).

But! I need to add elements to it, or remove elements from it.

  • Should I write a new kind of setter to only, do the task of adding instead of setting, like myLinkedList.add(someElement)?

  • Or it is OK to do this by using getter, without disobeying Encapsulation principal?

    getMyLinkedList().add(someElement)

( + Suppose I am going to lose my mark if I disobey encapsulation :-")

like image 634
Milad R Avatar asked Nov 04 '15 18:11

Milad R


People also ask

Should getter methods be private?

The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.

What is the point of getter methods?

The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute. Getters and setters allow control over the values. You may validate the given value in the setter before actually setting the value.

What is the advantage of using getter and setter?

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.

Do getters and setters enforce or weaken encapsulation?

Having getters and setters does not in itself break encapsulation.


1 Answers

I don't think it a particularly great practice to do something like:

myObj.getMyList().add(x);

since you are exposing a private class variable in a non read only way, but that being said I do see it pretty frequently(I'm looking at you, auto generated classes). I would argue that instead of doing it that way, return an unmodifiable list and allow users of the class to add to the list via an explicit method:

public class MyClass{
    private final List<String> myList = new ArrayList<String>();

    public List<String> getList(){
        return Collections.unmodifiableList(this.myList);
    }

    public void addToList(final String s){
        this.myList.add(s);
    }
}

EDIT After reviewing your comments, I wanted to add a bit about your setter idea:

I meant using that line of code inside a new kind of setter inside the class itself, like public void setter(someElement){this.myLinkedList.add(someElement);}

If I'm understanding you correctly, you are saying you want to expose a method that only adds to your list. Overall this is what I think you should be shooting for, and what many have outlined in the answers, however, labeling it as a setter is a bit misleading since you are not reassigning (setting) anything. That, and I strongly recommend returning a read only list from your getter method if possible.

like image 66
Nick DeFazio Avatar answered Oct 04 '22 01:10

Nick DeFazio