I have small doubt. I created my own LinkedList class and wrote all basic functions to it.
I built Linked list using those operations in my LinkedList class and am using this list in different methods in different classes.
When am calling those methods am giving Head of list as parameter and I am completely changing the Head in those methods.
But still I see my list is not disturbed. It is printing correct values when I print from my main class. I hope I have explained clear enough.
Please explain... Thanks in advance
You say you change some headers in your functions, but you change the instance variables, not the classVariables.
If you want to make changes for every use of the list you'll have to declare your variables as classvariables (making them public & static)
then you would direct them by YourClassName.variableName
It's good to be able to use someting, it's better to understand how it works.
If you create your own collection class, such as your "LinkedList", then the internal mechanisms by which you implemented the collection should be completely hidden from the user. For example: if your LinkedList class has a field called "head", then nowhere should you give external access to head that would allow it to be modified by the caller directly. Having a "getHead()" method should return the data referred to by your internal head variable, but not the head variable itself.
How are you permitting access to the data? By index? Via an iterator?
Loknath Lokeey wrote:. . . I hope I have explained clear enough. . . .
I am afraid you haven't given enough detail, but Carey has become suspicious that you have some design problems.
How much code do you have in your main method? Seeing you write “main class” suggests youi may have another design problem, doing too much in the main method.
Daniel Demesmaecker wrote: . . . declare your variables as classvariables (making them public & static) . . .
Why would you want any class variables? I think it is a mistake to make anything static without a good explanaton, and, as Carey has said, it is bad practice to have non‑private variables.
Post the code you wrote so people have a clear idea of what you're dealing with. Without code, others can only speculate and that doesn't lead to any concrete and definitive answers to your questions. Post your code and make sure to use code tags around it so it's readable.
Reading your post again, it seems you wanted to know whether or not parameters are passed by reference in Java. The short answer is no, parameters in Java are always passed by value, which is why you don't see your list "disturbed" when you pass in a reference to the head node and change it in your methods. As Others have noted, however, you should not require users of your linked list to pass in a reference to the head node. Tracking the head node is the job of your linked list and therefore should be a private instance variable that is hidden from any external code.