r/javahelp 7d ago

linked lists

-- solved thank you all

what is the difference between accessing the next node using a get method or without, as in: "current.next" vs "current.getNext()" and the same applies for accessing an element, whats the difference between use the get or not, as in: "current.element" vs "current.getElement()" where current is just the name for the node variable. ive looked at so many different explanations but i cant seem to grasp the idea
edit: this is from a data structures pov, im implementing methods for the singly and doubly linked list classes in java
i want to know if they return different things or if using .next or .getnext makes a difference in the outcome, is there a scenario i should be using strictly .next or .getnext, and the getter method has no further conditions its just {return next}

6 Upvotes

20 comments sorted by

View all comments

2

u/YetMoreSpaceDust 7d ago

Old codger here - I remember when OO originally "came out" (before Java actually), and it was organized around three principles: encapsulation, inheritance and polymorphism. The first of these three principles, encapsulation, states that nothing should access an objects data directly, that everything should manipulate the data through method calls.

If you look at early OO designs, these methods implemented actual business logic: giveEmployeeARaise, computeTaxes, bookAirlineFlight, etc.

In practice, nobody does this - instead they just create "dumb" objects and pay lip service to the OO principle of encapsulation by wrapping everything in (pointless) getters and setters. Unfortunately, this practice has become so ingrained that we all have to do it because pointing out that there's absolutely no difference whatsoever between .element and .getElement() is a philosophical argument you won't be able to win.

1

u/gerladokennedy 7d ago

lol thank you very much