Home >Java >javaTutorial >Why Am I Getting the \'error: \'.class\' expected\' Compilation Error in Java?

Why Am I Getting the \'error: \'.class\' expected\' Compilation Error in Java?

Linda Hamilton
Linda HamiltonOriginal
2024-11-26 19:24:131010browse

Why Am I Getting the

Understanding 'error: '.class' expected'

Error Description:

This error occurs during compilation when the compiler encounters a type name in a context where it expects an expression. This error message indicates that the compiler is confused and believes a .class expression is required at that location.

Causes:

  • Type Instead of Expression: The compiler expected an expression (e.g., a variable or method call) but encountered a type name (e.g., int or int[]).
  • Syntax Error: A separate syntax error may have triggered the compiler's confusion.

Fixes:

The solution depends on the intended code:

  • Type Cast: If you intended a type cast, use parentheses around the type:
double d = 1.9;
int i = (int) d; // Correct: cast 1.9 to integer
  • Remove Type: If you meant to assign or pass a value as-is, remove the type declaration:
int j = someFunction(a); // Correct ... assuming 'a' is appropriate

Additional Examples:

  • Missing parentheses:
someMethod(array[]);
  • Incorrect parameter syntax:
int i = someMethod(int j); // Should be: int i = someMethod(j);
  • Invalid typecast:
int i = int(2.0); // Should be: int i = (int) 2.0;
  • Syntactic error:
int[]; letterCount = new int[26];
  • Redundant semicolon:
if (someArray[] > 80) { // Should be: if (someArray[someIndex] > 80)
  • Insufficient curly brackets:
if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
    double cur = acnt_balc - (withdraw + 0.50);
    System.out.println(cur);
else
    System.out.println(acnt_balc);

The above is the detailed content of Why Am I Getting the \'error: \'.class\' expected\' Compilation Error in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn