Class Expected Error in Java

Haider Ali Jan 30, 2023 Aug 17, 2022
  1. Compile Time Error
  2. the '.class' expected Error in Java
  3. Fix the '.class' expected Error in Java
Class Expected Error in Java

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

What type of error is it, and when does it occur? Why does it occur, and what is the solution to it?

So without any further delay, let’s dive in.

Compile Time Error

A '.class' expected error is a compile-time error. It may be confusing for some people as they think this is a run-time error, but that is false.

A run-time error occurs when compilation is successful, but there was an issue at run time.

The compiler cannot detect run-time errors during compilation because not all the run-time information is given to the compiler. Thus, the '.class' expected error falls under compile-time because the code is written such that the compiler is confused.

the '.class' expected Error in Java

Whenever we write a code in Java, there are chances that we might find a .class expected error. Some might worry about what this is about when our code is seemingly perfect.

But this is not worrisome, as simple mistakes cause this error to appear like curly braces are missing, there is a semicolon issue or other syntax mistakes.

The syntaxes, curly braces, and semicolons play an important role in Java. We need curly braces to define a block of code and a semicolon to tell the compiler that we have finished a line of code to compile the next line.

In short, a .class error is an error that occurs if we make a Syntax Error.

Fix the '.class' expected Error in Java

Now, these mistakes can be fixed with a thorough evaluation. But some errors (Syntax Errors) are not known to the user and when the error '.class' expected appears, it causes both confusion and exhaustion as the user cannot figure out why this error occurred.

When an error occurs, the compiler usually suggests an option to fix it. It tells the user the problem and its solution, but in this case, '.class' expected is not a solution.

When the compiler suggests this, it’s usually a case with the syntax used in the code, as mentioned above. Including classes is not a solution; it’s just a compiler being confused about what to suggest to the user.

Here is an example of a sorting array used to sort the numbers given to the array.

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        int[] sorting = new int[] {4,8,2,6,7};

        Arrays.sort(sorting[]);
        System.out.println("Sorted array : ");

        for (int i = 0; i < sorting.length; i++)  // to print the array
        {
            System.out.println(sorting[i]);
        }
    }
}

Output:

error: '.class' expected
Arrays.sort(sorting[]);
                   ^
1 error

As shown in the above example, it is a simple sorting array. A simple mistake of square brackets [] caused this error to occur as the compiler could not understand where this line of code Arrays.sort(sorting[]); was written.

It is a Java syntax in which you cannot use empty square brackets. In this case, if we need to pass a variable, we have to use the variable only as there is no need to inform the compiler that this is an array.

We pass an array like any other object.

Solution:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        int[] sorting = new int[] {4,8,2,6,7};

        Arrays.sort(sorting);
        System.out.println("Sorted array : ");

        for (int i = 0; i < sorting.length; i++)  // to print the array
        {
            System.out.println(sorting[i]);
        }
    }
}

Output:

Sorted array :
2
4
6
7
8

As explained above, we received the error '.class' expected because of a square bracket, so we can execute our program as desired by simply removing the square bracket. This was due to the requirements of Java syntax.

The array sorting itself is an object whose type is int[]. Hence, the compiler doesn’t accept sorting[] being passed as an object.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java Error