Home > Java > javaTutorial > body text

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

Linda Hamilton
Release: 2024-11-26 19:24:13
Original
882 people have browsed it

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
Copy after login
  • 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
Copy after login

Additional Examples:

  • Missing parentheses:
someMethod(array[]);
Copy after login
  • Incorrect parameter syntax:
int i = someMethod(int j); // Should be: int i = someMethod(j);
Copy after login
  • Invalid typecast:
int i = int(2.0); // Should be: int i = (int) 2.0;
Copy after login
  • Syntactic error:
int[]; letterCount = new int[26];
Copy after login
  • Redundant semicolon:
if (someArray[] > 80) { // Should be: if (someArray[someIndex] > 80)
Copy after login
  • 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);
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template