Returning an array without assign to a variable

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

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 {}.


You been to construct the object that the function is returning, the following should solve your issue.

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

hope this helps

Tags:

Java

Arrays