r/javahelp • u/gerladokennedy • 22h 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}
3
u/tsvk 22h ago
When you refer to
current.next, you are accessing directly a member field variable callednextof the object reference calledcurrent. The visibility of the fieldnextis set to such a level in the objectcurrentthat you are able to access the field from outside the object, in other words the field is notprivate. 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 tocurrent.nextis possible it means that the field is not set toprivatebut something else, like package private (no visibility modifier) or evenpublic.On the other hand, when you call
current.getNext()you are calling a method calledgetNexton thecurrentobject. 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 callednext.