Returning an array without assign to a variable

Is there any way in java to return a new array without assigning it first to a variable? Here is an example:

public class Data {
    private int a;
    private int b;
    private int c;
    private int d;
    public int[] getData() {
        int[] data = { a, b, c, d };
        return data;
    }
}

I want to do something like this, but doesn't work:

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

Solution 1:

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

Solution 2:

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

Solution 3:

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

Solution 4:

return new Integer[] {a,b,c,d}; // or
return new int[] {a,b,c,d};