2
private static Coordinate[] getCircleCoordintaes() {  
  Coordinate coordinates[] = {new Coordinate(0, 0)};
  return coordinates;   
}

Above program working fine. In the above program for return the coordinate array first initialized the array using this line

Coordinate coordinates[] = {new Coordinate(0, 0)}; 

and then return coordinates.

But when I try to return directly below line then got exception.

{new Coordinate(0, 0)} 

Actually I am trying to find a way to return coordinate array directly. I want to skip the assigning step. May be I am doing something wrong.

How to return this array directly? Any suggestion?

1
  • 1
    return new Coordinate[] {new Coordinate(0, 0)};? If you want help resolving the exception then you need to tell us exactly what the exception was... Commented Sep 28, 2015 at 13:20

1 Answer 1

10
return new Coordinate[] { new Coordinate(0, 0) }

To elaborate, the construct you are using ({new Coordinate(0, 0)};) is called Array initilizer and as per JLS can be used only in declaration or as part of Array creation expression.

An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.

ArrayInitializer:
    { VariableInitializersopt ,opt }
Sign up to request clarification or add additional context in comments.

2 Comments

why return new Coordinate[] { new Coordinate(0, 0) } ? you are creating uncessary object, we can achevie this by return new Coordinate[]{0, 0};
@Ankushsoni You are creating and returning array of Coordinate objects in your example (assume that it is object holding x and y) properties. But this new Coordinate[]{0, 0} attempts to create Coordinate array and assign it two int values (0). Therefore this piece of code will not compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.