Why this Java code is showing identifier expected error? [closed]

Tags: , ,



I am learning Java and came across this problem. The problem statement is not much important here. While running the code I am getting an error. The code is

class constructor1
{
    Public static void main(String args[])
    {
        Example1 obj1 = new Example1(20,30);
        Example1 obj2 = new Example1(10,20);
        obj1.show();
        obj2.show();
    }
}
class Example1
{
    int a,b;
    Example1(int x,int y)
    {
        a = x;
        b = y;
    }
    void show()
    {
        system.out.println("a = " + a + "b =" + b);
    }
}

And the error is

    constructor1.java:3: error: <identifier> expected
        Public static void main(String args[])
              ^
1 error

I have searched many online sources to fix the error but unable to find it. Can someone help what is the reason for that error?

Answer

public is a keyword and case matters.

You wrote Public which the compiler didn’t understand as a keyword but thought you might be referencing a type called Public and thus is waiting for a variable name (thus “identifier expected”).

Change Public to public.



Source: stackoverflow