Ajay Kulkarni
Ajay Kulkarni

Reputation: 3039

Identifier expected in java

I wrote a simple program in java:

public class Will {

    int static square(int x) {
        int y = x * x;
        return y;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a number...");
        int n = sc.nextInt();
        int result = Will.square(n);
        System.out.println("Square of " + n + " is " + result);
    }
}

When I try to compile it, I'm getting those errors:

square.java:6: error: <identifier> expected
         int static square (int x)
            ^
square.java:6: error: invalid method declaration; return type required
         int static square (int x)
                    ^
2 errors

How to resolve those?

Upvotes: 0

Views: 442

Answers (2)

Bhargav Modi
Bhargav Modi

Reputation: 2655

Change it like

public/private/protected(Access specifier) static int square(int x) 

as per JAVA standard rules

Upvotes: 1

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62854

It has to be:

static int square (int x)

i.e the return-type has to be after the access-modifiers and before the method name.

Upvotes: 8

Related Questions