Read Also: [Fixed] Syntax error on token(s), misplaced construct(s)
As always, first, we will produce the syntax error on token "void", record expected error before moving on to the solution. Let's dive deep into the topic:
Note: This syntax-based compiler error can be produced only in the Eclipse IDE.
[Fixed] Syntax error on token "void", record expected
1. Producing the error by inserting import statements inside the class
We can easily produce this error in the Eclipse IDE by inserting import statements inside the class as shown below in the example:
public class SyntaxError {import java.io.*;public void getFile(String fileName) { } public static void main(String[] args) { } }
Output:
1.2. Explanation:
The cause of this error is due to inserting an import statement inside the Java class.
1.3. Solution:
The above compilation error can be resolved by moving the import statement outside of the Java class as shown below in the code:
import java.io.*;public class SyntaxError { public void getFile(String fileName) { } public static void main(String[] args) { } }
2. Producing the error by missing the classname declaration statement
We can easily produce this error in the Eclipse IDE by missing the classname declaration statement as shown below in the example:
import java.util.Scanner; public static void main(String[] args) { System.out.println("Alive is Awesome"); }
Output:
2.2. Explanation:
The cause of this error is by providing the method code without declaring the classname statement.
2.3. Solution:
The above compilation error can be resolved by adding the public classname statement and moving the method inside it as shown below in the code:
import java.util.Scanner;public class SyntaxError {public static void main(String[] args) { System.out.println("Alive is Awesome"); } }
Output:
Alive is Awesome
That's all for today. Please mention in the comments if you have any questions related to how to solve syntax error on token "void", record expected error in Java.