Java error return type required

[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 .

Читайте также:  Javascript var in hidden input

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.

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.

Was this post helpful?

Share this

Author

[Fixed] Unsupported class file major version 61 in Java

Table of ContentsReason for Unsupported class file major version 61 in JavaSolution for Unsupported class file major version 61 in JavaAndorid studio/Intellij Idea with gradleAny other scenario In this post, we will see how to fix Unsupported class file major version 61 in Java. Reason for Unsupported class file major version 61 in Java You […]

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

[Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Table of ContentsReason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListFixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListUse ArrayList’s constructorAssign Arrays.asList() to List reference rather than ArrayList In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to […]

HashMap values cannot be cast to list

[Fixed] java.util.HashMap$Values cannot be cast to class java.util.List

Table of ContentsWhy HashMap values cannot be cast to list?Fix for java.util.HashMap$Values cannot be cast to class java.util.List In this post, we will see how to fix error java.util.HashMap$Values cannot be cast to class java.util.List. Why HashMap values cannot be cast to list? HashMap values returns java.util.Collection and you can not cast Collection to List […]

Unable to obtain LocalDateTime from TemporalAccessor

[Fixed] Unable to obtain LocalDateTime from TemporalAccessor

Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

Источник

Fix Java Invalid Method Declaration; Return Type Required

Fix Java Invalid Method Declaration; Return Type Required

Invalid method declaration; return type required . This type of error occurs in Java when you declare a function and don’t mention its return type.

Let’s follow up on the basics of functions and methods in Java.

Fix Invalid method declaration; return type required in Java

You need to understand how to name and define methods in Java.

Let’s take a simple example of declaring a function. Our function will add two numbers, and it will return the answer, which will be of an integer value.

public int addTwoNumbers(int a, int b)   return a+b; > 

public is a reserved keyword in Java used to tell the member’s access. In this instance, it is public.

This keyword is followed by the return type of the method/function. In this case, it is int . Then you write the function’s name, and it can be any word of your choice provided it’s not a reserved keyword.

The above function will work just fine, and you will not receive any errors. But the error invalid method declaration; return type required occurs when you miss adding the function’s return type.

You can solve this by writing void instead of the return type. The void suggests that the function will not return any value.

public void displaystring(String A)   System.out.println(A);  return A;//wrong way  > 

As the above method is a void function, it cannot return a value. When you need to perform certain tasks, you use void functions, but you don’t require any value.

The correct way to write the above code is given below.

public void displaystring(String A)   System.out.println(A); > 

Here’s the complete self-explanatory code.

public class Main   public static void main(String args[])     // invalid method declaration; return type required This  // Error Occurs When you Declare A function did not mention any return type.   // there are only two options.  // if Function Did Not Return Any Value void Keyword should be used.  // void function always tell the compiler this function will return nothing..  Print();  Print1();  > // e.g of void function.  public static void Print()    System.out.println(" I am Void Function");  > // e.g of non void Function.   public static int Print1()    System.out.println(" I am Non Void Function");  return 3;  > > 
I am Void Function I am Non Void Function 

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

Related Article — Java Function

Related Article — Java Error

Copyright © 2023. All right reserved

Источник

[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:

[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(); > >

Example 3: Producing the error by missing comma in enum

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.

About The Author

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

Источник

Оцените статью