Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java syntax error on token.... Identifier expected after this token [duplicate]

I am getting Java

Syntax error on token "callMe", Identifier expected after this token

on below line of my program:

c1.callMe();
class Class2 {
    Class1 c1 = new Class1();
    c1.callMe();
}

public class Class1 {
    public void callMe() {
        System.out.println("I am called!!");
    }
}
like image 846
Nitin Avatar asked Feb 25 '15 14:02

Nitin


People also ask

What is meant by Syntax error on token in Java?

In computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language. For compiled languages, syntax errors are detected at compile-time. A program will not compile until all syntax errors are corrected.

How do I fix identifier expected?

we get the identifier expected error. To fix the error, remove semicolons from the enum values. Sometimes the error may be much larger.

What does token identifier expected mean?

The <identifier> expected error is a very common Java compile-time error faced by novice programmers and people starting to learn the language. This error typically occurs when an expression statement (as defined in [3]) is written outside of a constructor, method, or an instance initialization block.


1 Answers

Class1 c1 = new Class1();
c1.callMe();

Must be moved to a method, it can't be at the class definition level, else it makes no sense (when would your code be executed??):

public class Class2 {
    public void doSomething() {
        Class1 c1 = new Class1();
        c1.callMe();
    }
}
like image 69
jpo38 Avatar answered Nov 15 '22 06:11

jpo38