[Fixed] Invalid method declaration; return type required

In this post, we will see how to resolve invalid method declaration; return type required.

There can be multiple reasons for invalid method declaration; return type required issue.

Missing method return type

It is quite clear from error message that you are missing return type of the method. If you don’t want to return anything from the method, then you should set it’s return type of void.

Let’s understand with the help of example:

You will get compilation error at line 13 with error invalid method declaration; return type required.

Solution

In method setEmployeeDetails(), we did not specified return type. If it is not returning anything then its return type should be void.

Let’s change following line
public setEmployeeDetails(String name,int age) to
public void setEmployeeDetails(String name,int age)

Above code should compile fine now.

Missing comma in enum

This might sound strange but you can get this error when you are missing comma between enum type.

Let’s see with the help of example:

C:\Users\Arpit\Desktop>javac Number.java
Number.java:6: error: invalid method declaration; return type required
Two(2);
^
Number.java:6: error: illegal start of type
Two(2);
^
2 errors

Solution

If you notice enum types are not separated by the comma and this is the reason for this error.

C:\Users\Arpit\Desktop>javac Number.java

C:\Users\Arpit\Desktop>

As you can see error is resolved now.

Using different name for constructor

As you might know that constructor name should be same as class name in java. You might have put different name in constructor.
Let’s understand with the help of example:

You will get compilation error at line 9 with error invalid method declaration; return type required.

Solution

As you can see, constructor name is different than class name.
Let’s change following line
public EmployeeN(String name) { to
public Employee(String name) { This will fix compilation error.

That’s all about invalid method declaration; return type required in java.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.