.class expected error in Java
Hey folks! Hope you are constructing great programs.
I was running my Java program and came across the error .class expected error in java
Raise your hands, If the same error has occurred to you as well. So, do you want to know the possible cause for this error? Do you want to find a solution? To learn more, scroll down to the article.
Scroll for More Useful Information and Relevant FAQs
- Which type of error it is and why does it occur?
- What is the possible cause and solution for the Class, interface, or enum expected error in Java?
- To brush up on your knowledge on this subject, take the MCQ at the End of this Article.
- Feedback: Your input is valuable to us. Please provide feedback on this article.
Which type of error it is and why does it occur?
.Class
is a compiler time error. The error could occur for a variety of reasons. Here, we are going to shed light upon a few possibilities just to make you understand the error.
Suppose you are having a double value and you are trying to typecast it into an int. You are writing the below-given code snippet.
public class Main {
public static void main(String[] args) {
System.out.println("We are trying to know about .class expected in java");
System.out.println("We are typecasting double to int");
double num = 6.7;
int a = int num;
}
}
Now, when we run this code snippet, we get the following error.
The error occurs because in the 7th line of the code snippet, it is looking like a declaration rather than type casting as we missed to put the round brackets around the typecast datatype.
If we would write it like this, int i = (int) num;
, the error might get resolved.
public class Main {
public static void main(String[] args) {
System.out.println("We are trying to know about .class expected in java");
System.out.println("We are typecasting double to int");
double num = 6.7;
int a = (int) num;
}
}
Let's run this program and the output is
We are trying to know about .class expected in java
We are typecasting double to int
Yes, the program gets successfully executed and the error gets resolved. This is one possibility where you get this type of error. Let's check another possibility.
Let's make a program of adding two numbers.
public class Main {
public static void main(String[] args) {
int x = 50;
int y = 6;
int sum = x + y;
System.out.println(int sum); }
}
Let me run this program.
Oops! I get an error! But why so?
This is because here by mistake I wrote int sum. And yes! This is wrong. System.out.println (int sum);
is wrong. If I remove int from here in the above code snippet and make it as System.out.println(sum); // Print the sum of x + y
Result: 56
What is the possible cause and solution for the Class, interface, or enum expected error in Java?
So, just now we have seen above .class expected error, and we come across a similar type of error as well, which is class expected error in java or Class, interface, or enum expected
error. This is also a compile time error, which many occur because of many reasons.
The first reason could be missing curly braces
class expected error in java, occurs due to missing Curly bracket {}. If by mistake, you added an extra closing bracket at the end of the program, then this error might occur.
Let's run a simple program and check the output.
public class Main {
public static void main(String[] args) {
int num = 20;
System.out.println(num);
}
}
}
In this piece of code, we put an extra bracket in the last. Now, let's run this program.
Output:
error: class, interface, or enum expected
}
1 error
You can see that we get the same error. So, one possible reason for class, interface, enum, or record expected is the extra curly bracket at the end of the program. Now, It will run successfully once we run the above program by removing the extra curly bracket from the end of the program.
Now, let us move ahead and see another possibility of the same error. Another reason for error is, If we create a function outside the class, then the same error might occur.
function created outside of the class
Let's implement it using an example of inheritance and check. Here we are creating a function func outside the class in which we have mentioned some messages.
class fruit {
public void Fruitcolor() {
System.out.println("Every fruit has a color");
}
}
class apple extends fruit {
public void Fruitcolor() {
System.out.println("Apple is Red");
}
}
class mango extends fruit {
public void Fruitcolor() {
System.out.println("Mango is Yellow");
}
}
class Main {
public static void main(String[] args) {
fruit myfruit = new fruit();
fruit myapple = new apple();
fruit mymango = new mango();
myfruit.Fruitcolor();
myapple.Fruitcolor();
mymango.Fruitcolor();
}
}
public static void func() {
System.out.println("Learn the color of the fruits");
}
Now, let's run this program to check the output.
Output:
Solution: Define function within class scope
Another possible reason could be, If you are declaring more than one package in the same file, you might get the same error.
Declaring more than one package in the same file
Let's run a program and check. Here, we are declaring two different packages abc and xyz inside the same file.
package abc;
class abc {
void fun1() { System.out.println("TODAY WE ARE SOLVING AN ERROR"); }
}
package xyz;
public class xyz {
public static void main(String[] args)
{
System.out.println("Good Morning");
}
}
Let's run this program and check the output. Oops! Again we get the same error.
Brain Exercise
Was this post helpful?
- ?
Which method call will not result in a .class expected error?Try your hand at more Multiple-choice ExercisesOptions are: