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(); } }
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 (); } }
Output
Good Morning! Have a nice day
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(); } }
Output
Congratulations! Have a great professional life
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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) 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

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) 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

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