Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Identifier expected

Tags:

java

What's the issue here?

class UserInput {   public void name() {     System.out.println("This is a test.");   } }  public class MyClass {   UserInput input = new UserInput();   input.name(); } 

This complains:

<identifier> expected    input.name(); 
like image 601
randombits Avatar asked May 11 '12 22:05

randombits


People also ask

What does identifier expected mean in Java?

The identifier expected error is a compilation error, which means the code doesn't comply with the syntax rules of the Java language. For instance, one of the rules is that there should be a semicolon at the end of every statement. Missing the semicolon will cause a compilation error.

What is the identifier expected error?

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.

What is the meaning of class expected in Java?

class' expected. public class Calculator{ public Calculator(){ } public int sum(int one, int two) { int s = one + two; return int s; } } This error usually means that you are trying to declare or specify a variable type inside of return statement or inside of a method calls.


1 Answers

Put your code in a method.

Try this:

public class MyClass {     public static void main(String[] args) {         UserInput input = new UserInput();         input.name();     } } 

Then "run" the class from your IDE

like image 130
Bohemian Avatar answered Oct 05 '22 23:10

Bohemian