You still need to create the array, even if you do not assign it to a variable. Try this:

public int[] getData() {
    return new int[] {a,b,c,d};
}

Your code sample did not work because the compiler, for one thing, still needs to know what type you are attempting to create via static initialization {}.

Answer from Perception on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-return-an-array-in-java
How to Return an Array in Java? - GeeksforGeeks
July 23, 2025 - Multidimensional arrays in java can be said to be an array of arrays inside arrays. The simplest form can be a two-dimensional array. They have their sizes and declaration according to their sizes. Here returning of a two-dimensional array is demonstrated below that has a very similar approach ...
Discussions

How do you return an array in a method?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
26
15
April 22, 2021
How to return a temporary int array in Java - Stack Overflow
How can I manage to return a temporary array in Java (in order to save code line, without creating a variable). When doing initiation, I can use int[] ret = {0,1}; While when doing return, I cann... More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 29, 2016
memory management - How to return an array in one line in Java? - Stack Overflow
No-one should be surprised by that; itโ€™s the way it works since Java 1.1. 2016-06-08T18:03:23.993Z+00:00 ... No matter how you try to shorten your code, it doesn't change the fact that when the call to getCoord() returns, it simply returns a reference to the array, not a copy of the contents ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java array pass without reference - Stack Overflow
Try to receive an array in a method ... it will still be null, that's because Java passes by value, not the reference of the object. ... {10,3,4,12,2} is a syntactic sugar for array initialization, which must go with the declaration statement like the one in the following ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how do you return an array in a method?
r/javahelp on Reddit: How do you return an array in a method?
April 22, 2021 -

Today, I had a interview but I failed miserably in the first coding test. And before I can finish it, the interviewer just hung up the phone....sob sob

I am completely stumped how to do this question and have spent the last 2 hours figuring out to no avail and I hope to be able to get some helps here.

So, you are given an array = {1,2,6,4,5}

And a target no say 10.

The method must return the answer in {6,4}.

How can I achieve it ?

Here's my lame attempt:

public static int[][]ab(int[]arr, int target){
		target = 10;
		int[][]temp = null;
		int a = 0; int b = 0;
		int sum = 0;
		for(int i = 0; i < arr.length; i++) {
		for(int j = i; j < arr.length; j++) {	
			a = arr[i];
			b = arr[j];
			if ( a + b == 10) {				
				System.out.printf("Pair : " + arr[i], arr[j] );
			}
		}		
		}
return temp;// i do not know how to put arr[i] and arr[j] and store it in a array 
		}

Tks.

Top answer
1 of 8
11
Today, I had a interview but I failed miserably in the first coding test. And before I can finish it, the interviewer just hung up the phone....sob sob You should probably hold off on interviews for a while. You have a lot of basics to learn still, and should focus your time on learning those things rather than trying to interview for jobs you're not yet qualified for.
2 of 8
9
Why is your method trying to return a 2d array, when a 1d array is probably needed? I've put a cleaner version of your above method below, keeping the logic which is correct, adding a JavaDoc to better explain the problem which you are trying to solve (or, at least, what I think the problem you're trying to solve is supposed to be), fixing the method signature, and leaving a couple of TODOs for the stuff you haven't done correctly. Again, this is operating under the assumption that you needed to return a one-dimensional array, not a 2d array. /** * Given a target number and an input array, this method should * return an array of two integers, these integers being the two * numbers from the input array which add up to the 'target'. * @param arr the array containing the numbers which should add up to the 'target' * @param target we are looking for the numbers in 'arr' that add up to this * @return a two-index array containing the two numbers from 'arr' which 'target' is the sum of. */ public static int[] ab(int[] arr, int target){ // we will put the results in here when we find them final int[] results = new int[2]; for(int i = 0; i < arr.length; i++) { final int lhs = arr[i]; // the left hand side number we will be testing for (int j = i+1; j < arr.length; j++){ // i+1 is used so we don't test index i against itself final int rhs = arr[j]; // the right hand side number // TODO: work out if lhs + rhs are equal to 'target'. // TODO: If they are, put them in 'results' // TODO: but, if they are, how are you going to break out of the loop, now that you have a result? :thonking: } } // and we return the 'results' array. we can return it just like how we'd return any other data type. return results; }
๐ŸŒ
Quora
quora.com โ€บ Why-is-it-OK-to-return-an-array-in-Java-but-not-in-C
Why is it OK to return an array in Java but not in C? - Quora
Answer (1 of 5): We have first do understand how the two languages represent data: [code]a=b; a.x=2; b.x=3; [/code]What is the value of [code ]a.x[/code] now? * in JAVA will be 3 * In C will be 2 To have the same effect in C you must have [code ]a[/code] and [code ]b[/code] as pointers, so th...
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ how-to-return-an-array-in-java
How to Return an Array in Java? (from a Method) | FavTutor
September 26, 2024 - Learn how to return an array in java in this article with code. We also mentioned how to pass and return an array in java from a method.
Find elsewhere
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ how to return an array in java? a detailed guide (+examples)
How To Return An Array In Java? A Detailed Guide (+Examples)
January 2, 2025 - Similarities Between Java Vs. C++ ... Learn how to return an array in Java by creating the array inside a method, populating it with values if necessary, and using the return keyword to send it back to the calling code for further use.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ return-array-java
How to return an Array in Java
July 13, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ core java โ€บ java array โ€บ return empty array in java
How to return an empty array in java? (with example) | Java2Blog
October 11, 2023 - We can return the array like this: new int[]{} i.e. an integer array or declare first. This is a simpler method to follow the above approach. We will create an empty array of objects of class Employee and then return it.
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ java examples โ€บ how to return an array in java [practical examples]
How to return an Array in Java [Practical Examples] | GoLinuxCloud
February 22, 2022 - There are three different ways to return an array from a function in Java as listed below: Return an array of primitive type, Return an array of objects, Return a multidimensional array
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 408623 โ€บ java โ€บ return-array-method
how to return an array from a method (Beginning Java forum at Coderanch)
November 2, 2007 - Once your method and your variable "a" are declared as "int[]", you can just use "return a" to return [a reference to] the array. Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma. ... when i tried to compile the following program, i got an incompatible type error ...
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ how to pass / return an array in java
How To Pass / Return an Array In Java
April 1, 2025 - This tutorial will explain how to pass an array as an argument to a method and as a return value for the method in Java with simple examples.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java return array
How to Return Array in Java | Delft Stack
February 2, 2024 - At last, we return it using return newArray. We create an object of the ArrayReturningClass class and access the createNewArray() function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arrays-in-java
Arrays in Java - GeeksforGeeks
This Java program demonstrates how to pass an array to a method. An integer array arr is declared and initialized in the main method. The sum() method is called with arr as an argument. Inside the sum() method, all array elements are added using a for loop. The final sum is then printed to the console. As usual, a method can also return an array.
Published ย  2 weeks ago
๐ŸŒ
Studytonight
studytonight.com โ€บ java-examples โ€บ how-to-return-an-array-in-java
How to Return an Array in Java - Studytonight
January 28, 2021 - At the end of this tutorial, you will learn how to pass an array in Java Methods, How to perform an operation on them inside the method, and finally How to return an Array from the Java Methods. In a program given below, we created a method public static int[] getArray() which will return an array arr that assigned to arr which is declared in the method calling statement int[] arr = getArray(); and finally, the array will be printed.