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.

Avoid the following code:

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;
    }
}

Output:

I am Void Function
I am Non Void Function
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Function

  • Return a Boolean Method in Java
  • System.gc() for Garbage Collection in Java
  • Create Callback Functions in Java
  • Return Nothing From a Function in Java
  • Related Article - Java Error

  • Return a Boolean Method in Java
  • System.gc() for Garbage Collection in Java
  • Create Callback Functions in Java
  • Return Nothing From a Function in Java