Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[Solved] invalid method declaration; return type required

In this post, I will be sharing how to fix invalid method declaration; return type required error. This error is mostly faced by Java beginners. This error generally occurs when the name of the constructor is different from the name of the class or missing method return type. Let's dive deep into the topic:

Read Also:
Error: Identifier expected

[Fixed] Error: invalid method declaration; return type required


Example 1: Producing the error by using a different name for the constructor


Consider the following code:

public class HelloWorld {

public HelloWorld1(){

}

public static void main(String args[]) {
System.out.println("Constructor name is different than the Class name");
}
}

When you compile the above code, you will get the following compilation error:

Output:
/HelloWorld.java:3: error: invalid method declaration; return type required
public HelloWorld1(){
           ^
1 error

Explanation


If you observe the code, then you will find that the name of the constructor is different from the name of the class. Hence, the compilation error. The above error can be fixed by defining the constructor having the same name as its class as shown below.

Solution



public class HelloWorld {

public HelloWorld(){

}

public static void main(String args[]) {
System.out.println("Constructor name is different than the Class name");
}
}


Output:
Constructor name is different than the Class name

Example 2: Producing the error by missing method return type


Consider the following code:

public class HelloWorld {

// Missing return type
public printName(){

System.out.println("JavaHungry");
}

public static void main(String args[]) {
HelloWorld obj = new HelloWorld();
obj.printName();
}
}


When you will compile the above code using the javac command, you will get the following compilation error:

Output:
/HelloWorld.java:4: error: invalid method declaration; return type required
public printName(){
           ^
1 error


Explanation


If you observe the code, then you will find that the return type of the method printName() is missing. Hence, the compilation error. The above error can be fixed by adding the return type in the method signature. Since method printName() does not return anything, hence, its return type is void.

Solution



public class HelloWorld {

public void printName(){

System.out.println("JavaHungry");
}

public static void main(String args[]) {
HelloWorld obj = new HelloWorld();
obj.printName();
}
}


Output:
JavaHungry

Example 3: Producing the error by missing comma in enum


Consider the below code:

public class EnumExample {
public enum Company {
APPLE("iPhone");

SAMSUNG("Galaxy");

private final String model;

private Company(String model) {
this.model = model;
}
}

public static void main(String args[]){
for(Company c : Company.values())
System.out.println(c);
}
}


When you compile the above class using the javac command i.e javac EnumExample.java, then you will find the following compilation error:

Output:
/EnumExample.java:4: error: invalid method declaration; return type required
SAMSUNG("Galaxy");
^
/EnumExample.java:4: error: illegal start of type
SAMSUNG("Galaxy");
                     ^
2 errors


Explanation


If you observe the code, then you will find that the comma is missing in the enum Company. Since enum types should be separated by the comma, that is not the case here. Hence, the compilation error. The above error can be fixed by adding the comma in the enum as shown below.

Solution



public class EnumExample {
public enum Company {
APPLE("iPhone"),

SAMSUNG("Galaxy");

private final String model;

private Company(String model) {
this.model = model;
}
}

public static void main(String args[]){
for(Company c : Company.values())
System.out.println(c);
}
}


Output:

APPLE
SAMSUNG

That's all for today, please mention in the comments in case you know any other way of solving invalid method declaration; return type required error.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

[Solved] invalid method declaration; return type required

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×