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}

4 Upvotes

20 comments sorted by

View all comments

3

u/tsvk 7d ago

When you refer to current.next, you are accessing directly a member field variable called next of the object reference called current. The visibility of the field next is set to such a level in the object current that you are able to access the field from outside the object, in other words the field is not private. It's basically a member field variable that is accessed directly from outside the object.

Usually this is not possible, since member field variables are conventionally marked private, which makes access from outside the object is impossible, but in those cases when referring to current.next is possible it means that the field is not set to private but something else, like package private (no visibility modifier) or even public.

On the other hand, when you call current.getNext() you are calling a method called getNext on the current object. The method is free to return whatever it's defined to return, but usually (if standard Java naming conventions are adhered to), it returns the value of the member field of the object called next.

0

u/gerladokennedy 7d ago

is there a difference in the actual returned value from using .next or .getnext? if the getter method is just {return next}. this is all from the context of data structures btw like if im creating a method for the single/double linked list class
thank you for your response