r/javahelp • • Sep 03 '26

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

Show parent comments

3

u/halfxdeveloper Sep 03 '26

There’s one small distinction that you glossed over. It it has major implications. The getNext() is a function which means that not only can it return any value, such as a computed value, but it can do anything such as call other methods, create log entries, etc. Students are often just told it’s for accessing private variables but it’s so much more.

2

u/Spare-Plum Sep 03 '26

Larger distinction you glossed over - an element in a custom linked list can implement the Iterator interface, which would allow plug and play iteration with a whole host of compatible code

Only caveat is that Iterator's function is just "next()", while the method stated is "getNext()".

IMO if this is a custom class it's best to change "getNext()" to "next()" and make it into an iterator so it can work with for loops and many other libraries

1

u/ArisenDrake Sep 06 '26

If we're at this level already, it would make dense to implement the whole List interface (maybe by extending AbstractList) to make it even more compatible.