Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Java compiler error message "<identifier> expected" mean?

class if{
    public static void main (String args[]){
        int x = 9;
        if (x <= 9){
            System.out.println("Yay");
        }else{
            System.out.println("Yay");
            }
        }
    }

I'm running this from the compiler, using Notepad++ as the text editor. And I am getting an error in the compiler saying <identifier> expected class if. And another error saying illegal start of expression. As well as saying error ";" expected. I have a total of 9 errors.

I made sure to match all the {} and (). Even scraped the program and tried again with the same results.

like image 637
Tarrant Avatar asked Aug 17 '11 21:08

Tarrant


Video Answer


2 Answers

if is a reserved keyword in Java (as seen in your if statement), and is thus not an eligible class name. Choose another name for your class, like IfTesting.

By convention, all class names start with an upper-case letter. The full details for what is and isn't a valid Java identifier are found in the Java Language Specification. In short, it can't be a keyword, true, false, or null.

like image 145
Mark Peters Avatar answered Sep 28 '22 16:09

Mark Peters


You shouldn't call a class "if". It's a reserved Java keyword (that you're using in your program, BTW).

Furthermore, by convention, all classes start with an uppercase letter in Java.

like image 25
JB Nizet Avatar answered Sep 28 '22 15:09

JB Nizet