vApor
vApor

Reputation: 23

How to fix "type annotations are illegal here" while passing int array argument?

I created a method which takes integer array as parameter. However I'm getting an error "Type annotations are illegal here" while passing the argument into the form {int1, int2, int3}.

I tried creating an integer array first, assigning it a value and then passing the array and it worked just fine. The parameter variable is getting created when the method is being called and hence it should accept the {int1, int2, int3} array method of passing argument. I've searched about "Type annotations are illegal here" error, but couldn't find any relevant information.


public class Game {

    public static void main(String[] args) {
        int noOfGuesses;
        String result = "";
        int startLoc = 1;

        Battlefield dot = new Battlefield();

        dot.setLocation({startLoc,startLoc+1,startLoc+2});

I got an error pointing at dot.setLocation(...) method call.

public class Battlefield {
    int noHits=0;
    int position[];

    void setLocation(int startPosition[])
    {
        this.position= startPosition;
    }

The code above is setLocation() definition.

Upvotes: 1

Views: 3470

Answers (1)

mernst
mernst

Reputation: 8137

The syntax for a literal array is new int[]{1, 2, 3};. As a special case, you are allowed to write {1, 2, 3} in a variable declaration, as in:

int[] myIntArray = {1, 2, 3};

However, the special case syntax {1, 2, 3} is not permitted in other locations, such as in an argument position (where you tried to write it).

The error message "type annotations are illegal here" is misleading, and your problem has nothing to do with type annotations.

Upvotes: 4

Related Questions