Table of Contents
Syntax
Example
Output
in conclusion
Home Java javaTutorial Java program example: a method with no parameters and return type

Java program example: a method with no parameters and return type

Sep 17, 2023 am 11:13 AM
java method No ginseng No return

Java program example: a method with no parameters and return type

First, let’s get familiar with the syntax, examples, and finally the implementation.

Methods in Java are very important because they allow the same code to be reused, reducing the number of statements that need to be written in the code.

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

  • Declaration of method.

  • Definition of method.

  • Call this method.

Method invocation is the last step, while the other two steps are interchangeable. The only thing to remember is that you must declare a method before calling 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();
   }
}
Copy after login

A method to create an empty parameter list in a class. Statements are written inside the method, and an empty return statement may be added at the end. Then call this method in the main method.

Example

The following program is to show how to create a method with neither parameters nor return type.

A class named Wish is created within which, a named method 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 ();
	}
}
Copy after login

Output

Good Morning! Have a nice day
Copy after login

Example

The following program is to show how to create a method with neither parameters nor return type.

A class named Wish is created within which, a named method 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();
	}
}
Copy after login

Output

Congratulations! Have a great professional life
Copy after login

in conclusion

This article explains how to define a method without parameters and return value in Java. We started with the syntax and further saw an example and two Java programs to get a clear understanding of the topic.

The above is the detailed content of Java program example: a method with no parameters and return type. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve Java method not found (NoSuchMethodError) error How to solve Java method not found (NoSuchMethodError) error Aug 20, 2023 am 08:15 AM

How to solve the Java method not found (NoSuchMethodError) error During the Java development process, we often encounter various errors. One common error is NoSuchMethodError, which means that the corresponding method cannot be found. This error is generally caused by version incompatibility or changes in dependencies. Here are some ways to solve the Java method not found error. Check version compatibility NoSuchMethodError error is usually caused by

How to solve Java method return value exception (IllegalReturnValueException) How to solve Java method return value exception (IllegalReturnValueException) Aug 17, 2023 pm 09:09 PM

How to solve Java method return value exception (IllegalReturnValueException) In Java programming, we often encounter method return value exception (IllegalReturnValueException). This exception is usually caused by the method return value not matching the method declaration or returning an illegal value. This article will introduce the causes and solutions of common IllegalReturnValueException, and provide

Share a simple method to solve the exception of Java reading large files Share a simple method to solve the exception of Java reading large files Feb 19, 2024 pm 09:42 PM

A simple method to solve Java large file reading exception is shared. During the Java development process, sometimes we need to handle the reading operation of large files. However, because large files occupy a large amount of memory space, abnormal situations such as memory overflow often occur. This article describes a simple workaround, along with specific code examples. When processing large files, we usually use segmented reading to divide the file into multiple smaller parts for processing to avoid loading the entire file into memory at once. Here is a simple example showing how to

How to solve Java method not implemented exception (MethodNotImplementedException) How to solve Java method not implemented exception (MethodNotImplementedException) Aug 18, 2023 pm 08:55 PM

How to solve Java method not implemented exception (MethodNotImplementedException) In Java development, sometimes you will encounter method not implemented exception (MethodNotImplementedException), which is a common mistake. This exception is thrown when we declare an abstract method or a method in an interface but do not implement the method in its concrete implementation class. This article will introduce how to solve Java method not implemented exception

Java program example: a method with no parameters and return type Java program example: a method with no parameters and return type Sep 17, 2023 am 11:13 AM

First, let's get familiar with the syntax, examples, and finally the implementation. Methods in Java are very important because they allow reuse of the same code, reducing the number of statements that need to be written in the code. There are three main parts of the method to make it executable. method declaration. Definition of method. Call this method. Method invocation is the last step, while the other two steps are interchangeable. The only thing to remember is that you must declare a method before calling it. SyntaxTocreateamethodwithoutanyparameterandreturntype,thefollowingsyntaxisconsidered.Classclass_name

How to solve Java method return value invalid exception (InvalidReturnValueException) How to solve Java method return value invalid exception (InvalidReturnValueException) Aug 18, 2023 pm 10:17 PM

How to solve the Java method return value invalid exception (InvalidReturnValueException) Background: In Java programming, we often encounter the method return value invalid exception (InvalidReturnValueException). This exception is usually caused by a method not returning the correct value. In this article, I will cover some common causes and solutions to help you solve this problem. Missing return statement When a method declares a return value type,

How to solve Java method invocation rejected exception (MethodInvocationRejectedException) How to solve Java method invocation rejected exception (MethodInvocationRejectedException) Aug 20, 2023 pm 05:53 PM

How to solve the Java method invocation rejected exception (MethodInvocationRejectedException). In the process of using Java development, we often encounter various abnormal situations. Among them, Java method invocation rejected exception (MethodInvocationRejectedException) is a common exception. This article will introduce what MethodInvocationRejectedExcepti is

Methods to solve the abnormal number of Java method parameters (InvalidMethodParameterCountException) Methods to solve the abnormal number of Java method parameters (InvalidMethodParameterCountException) Aug 26, 2023 pm 08:24 PM

Methods to solve the Java method parameter number exception (InvalidMethodParameterCountException) In Java programming, we often encounter exceptions where the number of method parameters does not match, that is, InvalidMethodParameterCountException. This exception usually occurs when the number of parameters passed in when calling a method is inconsistent with the number of parameters defined by the method. In order to solve this exception, we can take the following approach

See all articles