The code looks fine but not so. Because the print statement is not a proper place. It should be inside a method/ block. Let's wrap the code inside a method and then compile and run.
public class IdentifierError
public static void main(String args[])
System.out.println("javatpoint");
javatpoint
Reasons to Occur Error
There may be the following reasons to occur the error:
It occurs when the code does not comply with the Java syntax rules.
A block of code directly written in the class body instead of inside a method or block.
There may be extra curly braces.
The code is not at the proper place.
Every statement must have a semicolon at the end.
How to fix/ resolve errors?
Do not forget to put a semicolon at the end of the statement.
Do not put code directly inside the class body.
Calling of methods must be inside a method, constructor, or static initializer.
Write a block of code at the proper place.
Remove extra curly braces.
IdentifierErrorExample1.java
public class IdentifierErrorExample1
public static void main(String args[])
System.out.println("Python");
System.out.println("MySql");
System.out.println("Java");
Let's compile the above code. We get the <identifier> expected error.
Observe the above code, we get there is an extra curly brace that is the reason to generate an error. The error can be fixed by removing an extra brace at line 6.
The error also occurs when we put semicolon instead of comma while defining values in enum. For example, consider the following code.
IdentifierErrorExample2.java
public class IdentifierErrorExample2
public enum Vegetables
eggplant,
tomato,
broccoli;
public static void main(String args[])
for(Vegetables veg : Vegetables.values())
System.out.println(veg);
Let's run the above code. we get the identifier expected error.
To fix the error, remove semicolons from the enum values.
public enum Vegetables
eggplant,
tomato,
broccoli;
Sometimes the error may be much larger. Consider the following code.
IdentifierErrorExample3.java
import java.util.Arrays;
public class IdentifierErrorExample3
int[] primes = {17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73};
int max_val;
max_val = nums[0];
for (int i = 1; i < primes.length; ++i)
if (primes[i] > max_val)
max_val = primes[i];
System.out.println("Primes Numbers are: " + Arrays.toString(primes));
System.out.println("Largest Prime is: " + max_val);
Let's compile the above code.
We get too many errors because some statements directly write inside the class body. To resolve the error, write entire the block of code inside a method and then compile and run.
IdentifierErrorExample4.java
import java.util.Arrays;
public class IdentifierErrorExample4
public static void main(String args[])
int[] primes = {17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73};
int max_val;
max_val = primes[0];
for (int i = 1; i < primes.length; ++i)
if (primes[i] > max_val)
max_val = primes[i];
System.out.println("Primes Numbers are: " + Arrays.toString(primes));
System.out.println("Largest Prime is: " + max_val);
Output:
Next TopicJava Generate UUID