Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return reference to object as parameter in Java

Although I've programmed C, C++ and C# for many years I'm am only superficially familiar with Java. Helping my Comp Sci son with a Java college project he had a need to return references to two objects from a method in Java. I suggested returning one as the function value and the 2nd as a reference. He wasn't sure how to do this. I did a little research and realized it may not be possible. My question is in Java what is the common method used when a method needs to return more than one reference to an object. Here's the specific example in my sons case.

// This method returns references to the head and tail objects from the passed in 
// linked list.  The head object is returned as the function value and the tail is 
// returned as a parameter.
public Static Node GetHeadTail(List list, Node tail)

I realize the above doesn't work in Java since the tail is a reference to node and in Java the reference itself is passed by value. What is the most common way of dealing with this in Java? My son's solution was to return an array of 2 Node objects for the function value. I said that was a poor solution because it doesn't document the meaning of each element of the array. Another solution would be to create an object that contained the head and tail references. However in the particular example it was the head pointer that was of most interest and if an object was returned it would create undesired coding overhead for the caller of the method if all they wanted was the head.

like image 619
JonN Avatar asked Nov 16 '14 00:11

JonN


People also ask

How to return an object from a method in Java?

How to Return Object from a Method in JAVA. 1 Completes all the statements in the method. 2 Reaches a return statement. 3 or Throws an exception (covered later)

What are object references in Java?

This topic explains the concept of an object references; it is targeted at people who are new to programming in Java. You should already be familiar with some terms and meanings: class definition, main method, object instance, and the calling of methods “on” an object, and passing parameters to methods.

How do you define a parameter in Java?

Java Reference Java Keywords. ... Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. The following example has a method that takes a String called fname as parameter.

Can We pass objects as parameters to methods in Java?

Till yet, we have only been using the simple types as parameters to methods. However, it is both correct and common to pass objects to methods. For instance, look at the following short Java program :


2 Answers

In this case, java programmers would commonly create a class with 2 members: head and tail. That would be the return type for the getHeadTail(List list) method.

like image 94
Asaph Avatar answered Sep 28 '22 04:09

Asaph


You can only pass by value in Java. Your best solution is the second one your son suggested, i.e. return an object that has the head and tail.

like image 35
Ryan Hartman Avatar answered Sep 28 '22 05:09

Ryan Hartman