. Advertisement .
..3..
. Advertisement .
..4..
We will show you how to solve the invalid method declaration; return type required in this post. This is a common blunder among Java newbies.
This issue usually happens when the constructor name differs from the class name or when a method return type is missing. Let’s take a closer look at the subject:
Method Return Type Is Missing
The error notice makes it apparent that the method’s return type is lacking. Set the method’s return type to void if you don’t want anything to be returned from it.
Let’s look at an example to assist you understand:
public class Employee {
String name;
int age;
public Employee(String name) {
this.name = name;
}
public setEmployeeDetails(String name,int age)
{
this.name=name;
this.age=age;
}
}
At line 9, you will get a compilation error with the message invalid method declaration; return type required.
Solution
We didn’t provide a return type for the function setEmployeeDetails(). Its return type should be void if it is not returning anything.
Change the line after that public setEmployeeDetails(String name,int age)
to public void setEmployeeDetails(String name,int age)
.
public class Employee {
String name;
int age;
public Employee(String name) {
this.name = name;
}
public void setEmployeeDetails(String name,int age)
{
this.name=name;
this.age=age;
}
}
The code above should now compile successfully.
Enum Has A Comma Missing
This may seem unusual, but this issue can occur when a comma is absent between enum types.
Let’s have a look at an example:
public enum Number {
One(1);
Two(2);
private final int num;
Number(int i) {
this.num = i;
}
public int getNumber() {
return num;
}
}
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
This problem is caused by the fact that enum types are not separated by a comma.
Solution
public enum Number {
One(1),
Two(2);
private final int num;
Number(int i) {
this.num=i;
}
public int getNumber() {
return num;
}
}
C:\Users\Arpit\Desktop>javac Number.java
C:\Users\Arpit\Desktop>
As you can see, the problem has been rectified.
Using A Different Constructor Name
As you may be aware, in Java, the constructor and class names must match. You may have given the constructor an alternative name.
Consider the following scenario:
public class Employee {
String name;
int age;
public EmployeeN(String name) {
this.name = name;
}
}
At line 5, you will get a compilation error with the message incorrect method definition; return type needed.
Solution
Constructor names differ from class names, as you can see.
Let’s update the line
public EmployeeN(String name) {
to
public Employee(String name) {
This will resolve the compilation issue.
Invalid Method Declaration; Return Type Required: Example
Instance: Creating the problem by giving the constructor a new name.
Consider the code below:
public class HelloWorld {
public HelloWorld1(){
}
public static void main(String args[]) {
System.out.println("Constructor name is different than the Class name");
}
}
The following compilation error will appear when you build the aforementioned code:
Output:
/HelloWorld.java:3: error: invalid method declaration; return type required
public HelloWorld1(){
^
1 error
Explanation
If you look at the code closely, you’ll see that the constructor has a different name than the class. As a result, there is a compilation error. The aforementioned mistake may be resolved by naming the constructor the same as the class, as illustrated below.
Solution
public class HelloWorld {
public HelloWorld(){
}
public static void main(String args[]) {
System.out.println("The name of the constructor differs from the name of the class");
}
}
Output:
The name of the constructor differs from the name of the class
Conclusion
That’s all for now; please let us know if you have any other suggestions for dealing with the invalid method declaration; return type required error in the comments.
The function of
Voter.setDetails
has no return type. If you don’t want it to return specify the return type asvoid
In the error message says it clearly and you have to give return type of each method. If there is no return type just give void.