[Solved] Syntax error on token "void", record expected

In this post, I will be sharing how to fix syntax error on token "void", record expected error in Java. It is a compile-time error and mostly troubles Java beginners.

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:
syntax error on token void, record expected

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:
Syntax error on token "void", record expected missing class

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.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry