Java Program to Illustrate a Method without Parameters and Return Type


First, let us get acquainted with the syntax, and examples, and then finally the implementation.

The methods in Java are of great importance since it allows reusability of the same code, reducing the number of statements to be written within the code.

There are three main parts of the method in order to make it executable.

  • Declaration of the method.

  • Definition of the method.

  • Invoking the method.

Method invoking is the last step, while the other two can be interchanged. The only thing to be kept in mind here is, the method must be declared before invoking it.

Syntax

To create a method without any parameter and return type, the following syntax is considered.

Class class_name{
   function _name() {
      Statement 1;
      Statement 2;
      .
      .
      Statement n;
      //an optional return 
      return;
   }
   Main function() {
      // invoking the above function
      function_name();
   }
}

A method is created within a class with an empty parameter list. The statements are written inside the method which may be followed by an empty return. The method thus created is invoked in the main method.

Example

The following program is written to show how a method is created that neither has parameters nor any return type.

A class named Wish is created within which, a method named wish() with return type void is created denoting it does not return any value, also it does not consist of any parameter. A statement is written within the wish() method and is displayed by invoking this method in the main method.

// Java Program to demonstrate a method without Parameters and Return Type
public class Wish {
	// Declaration and Definition of the method
	public static void wish(){
		System.out.println("Good Morning! Have a nice day");
	}
	public static void main(String args[]){
		// Calling the method without any parameters
		wish ();
	}
}

Output

Good Morning! Have a nice day

Example

The following program is written to show how a method is created that neither has parameters nor any return type.

A class named Wish is created within which, a method named wish() with return type void is created denoting it does not return any value, also it does not consist of any parameter. The statements written inside the wish() method are displayed by invoking the method in the main method.

// Java Program to demonstrate a method without Parameters and Return Type
public class Wish {
	// Declaration and Definition of the method
	public static void wish(){
		System.out.println("Congratulations! Have a great professional life");
			//It is optional to use a return statement here.
			return;
	}
	public static void main(String args[]){
		// Calling the method without any parameters
		wish();
	}
}

Output

Congratulations! Have a great professional life

Conclusion

This article throws light on how a method is defined in Java without any parameters and return value. We started with the syntax and further saw an example and two java programs to have a clear picture of the topic.

Updated on: 11-Apr-2023

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements