error: class, interface, enum, or record expected in java
While compiling the program, showing the following error
ab.java:2: error: class, interface, enum, or record expected
Class A
^
ab.java:4: error: class, interface, enum, or record expected
public static void main(String args[])
^
ab.java:7: error: class, interface, enum, or record expected
for(int i=0;i<a.length;i++)
^
ab.java:7: error: class, interface, enum, or record expected
for(int i=0;i<a.length;i++)
^
ab.java:7: error: class, interface, enum, or record expected
for(int i=0;i<a.length;i++)
^
ab.java:10: error: class, interface, enum, or record expected
}
^
6 errors
There is not one reason for the above error but there can be many reasons for this error, here is a small mistake.
The following first program is getting compile time error.
The second program compiles and runs fine.
Here is the first and second simple program and a deeper understanding of this error.
//error: class, interface, enum, or record expected
Class A
{
public static void main(String args[])
{
int[] a={1,2,3,4,5};
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
class A
{
public static void main(String args[])
{
int[] a={1,2,3,4,5};
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
Did you find any difference?
So let’s analyze this simple Java program.
In this, the first program is getting compile time error and the second one compiles and runs properly.
The first program errors out because the class keyword’s first letter is written in capital. That’s why this kind of error is happening.
Java is case-sensitive, so if you use another keyword, check it first. This error may occur.