Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Is calling a method "by its reference" in Java ?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

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
 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5636
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have doubts about your doubts.

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?
 
Marshal
Posts: 63409
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Sheriff
Posts: 13160
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 13160
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I like tacos! And this tiny ad:
ScroogeXHTML 8.2 - easy to use RTF to HTML converter library
https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!