How to return multiple objects from a Java method

0 votes
What are the possible ways to return the number of objects from a method in Java?
Jul 12, 2018 in Java by whatsinname
• 280 points
3,358 views

2 answers to this question.

0 votes

Yes, we can return multiple objects from a method in Java as the method always encapsulates the objects and then returns.
You can return a list of objects like:

public class CustomObject<Obj> {
  public final int id;
  public final Obj customObject;

  public CustomObject(int id,Obj customObject) {
    this.id = id;
    this.customObject = customObject;
  }
}

and return this as List<CustomObject<DataType you want>>

Hope this helps!!

If you need to know more about Java, join our Java certification course online.

Thanks!!

answered Jul 13, 2018 by Mohini
• 260 points
0 votes

In the event the method you're calling is private, or called from one location, try

return new Object[]{value1, value2};

The caller looks like:

Object[] temp=myMethod(parameters);Type1 value1=(Type1)temp[0];  //For code clarity: temp[0] is not descriptiveType2 value2=(Type2)temp[1];
answered Aug 30, 2019 by Karan
• 19,620 points

Related Questions In Java

0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
1,489 views
0 votes
1 answer

How to download and save a file from Internet using Java?

public void saveUrl(final String filename, final String ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
292 views
0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,170 points
4,443 views
0 votes
1 answer

How can we return a JSON object from a Java Servlet?

response.setContentType("application/json"); // Get the printwriter object from response ...READ MORE

answered Jul 6, 2018 in Java by sharth
• 3,350 points
4,663 views
0 votes
1 answer

How to call a method from Constructor?

Have a glance at below steps: You create ...READ MORE

answered Nov 13, 2018 in Java by Frankie
• 9,810 points
1,660 views
0 votes
1 answer

How to run a Java program from the command line on Windows?

Hello @kartik, In case your Java class is ...READ MORE

answered Apr 9, 2020 in Java by Niroj
• 82,880 points
238 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 51,606 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
6,082 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
13,285 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
2,607 views