HOWTO · Java

Class Expected Error in Java

In this guide, we will look into the .class expected error in Java.

On this page

The Java compiler’s .class expected diagnostic does not mean that you should add a .class literal to every line it points to. Its meaning depends on the source context. In the example below, the problem is the sorting[] argument: Java permits [] in an array type such as int[], but the expression passed to a method is the existing variable sorting.

What the .class expected Error Means

This is a compile-time diagnostic. The compiler reports it while parsing or compiling source, before the program can run. A compiler message often includes a source line and a marker near the location where parsing stopped. That location is useful evidence, although the underlying mistake can be just before the marker, such as an unexpected bracket or a missing delimiter.

The words .class expected describe what the compiler expected in the particular parsing context. They do not establish one universal cause, and they do not mean that every Java syntax problem produces this diagnostic. Here, the relevant context is an array argument to Arrays.sort: the variable already names an array value, so adding empty brackets after it is not valid expression syntax.

Reproduce the Error with sorting[]

The expression sorting[] is the part to focus on. In a declaration, int[] sorting uses brackets as part of the array type. In a method call, sorting refers to the array object. The brackets are not a request to pass an array’s type or to create a new array.

For example, this call contains the mistake: Arrays.sort(sorting[]);. The supplied compiler diagnostic is .class expected; exact wording and line numbers can vary between Java compiler versions and surrounding source. The important clue is that the marker points into the invalid argument. Adding .class there would change the expression and would not sort the array.

Fix the Array Argument

Pass the array variable itself: Arrays.sort(sorting);. The following complete program demonstrates the corrected form. It uses a local array, sorts it in place, and formats the values for display.

Run this example:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] sorting = {3, 1, 2};
        Arrays.sort(sorting);
        System.out.println(Arrays.toString(sorting));
    }
}
[1, 2, 3]

Arrays.sort accepts the array value, and the same variable refers to the sorted array afterward. The key correction is deleting the trailing [] from the argument, not changing the array declaration or adding a class literal.

An empty array is also a valid input to this overload. This corrected example was executed successfully and printed an empty list:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] sorting = {};
        Arrays.sort(sorting);
        System.out.println(Arrays.toString(sorting));
    }
}
[]

The same call accepts the array variable when it contains no elements. The declaration still uses int[]; the method argument remains sorting.

Diagnose the Compiler Location

When the diagnostic appears, use a short, repeatable inspection process:

  1. Read the complete compiler message and note the file, line, and marker. Treat the marker as a starting point, because parsing can fail after the original typo.
  2. Inspect that line and the line immediately before it. Check whether brackets, parentheses, commas, and semicolons match the surrounding expression.
  3. Identify whether the code is declaring a type or using a value. int[] sorting is a declaration; Arrays.sort(sorting) is a method call using the variable.
  4. Compare the expression with the method’s expected argument. In this example, Arrays.sort receives the array reference, not bracket syntax after the variable.
  5. Recompile after the edit and read the new diagnostic, if any. Fix the first reported problem and repeat; one syntax problem can obscure a later one.

Do not infer that an extra brace, a misplaced method, an import, or a filename mismatch caused this specific message unless the compiler location and source support that diagnosis. Those issues can produce other compiler errors. Keep the investigation anchored to the exact source and message rather than applying a generic .class fix.

Verify the Corrected Program

After changing the argument to sorting, compile the source and run the class using the commands appropriate for your project and installed JDK. The program’s sort call changes the array in place; Arrays.toString turns its elements into readable text. The supplied execution evidence confirms that both corrected examples ran successfully: the populated array printed [1, 2, 3], and the empty array printed []. This evidence covers the corrected calls only; it does not establish that the erroneous sorting[] call was executed.

If the same diagnostic remains, verify that the edited file is the one being compiled and that the reported line matches the current source. Then inspect nearby punctuation and the complete call, including its opening and closing parentheses. Compiler wording and line references can differ by version, so use them together with the code rather than relying on the message alone. For the demonstrated issue, the fix remains specific: declare the array with [] in its type, then pass the array variable as sorting.