Java Program to Illustrate a Method without Parameters but with Return Type


In Java, a method is a block of code that performs a specific task. Methods can take zero or more parameters and can also return a value. When a method returns a value, we call it a method with a return type.

A method without parameters but with a return type is a method that does not take any parameters, but it returns a value. This type of method is useful when we need to perform a specific task that does not require any input parameters, but we need to get a result from that task.

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

Syntax

The syntax for defining a method without parameters but with a return type is as follows −

class class_name {
   data_type method_Name() {
      Statement 1;
      Statement 2;
      ..
      ..
      Statement n;
      return value/variable;
   }
}

Here,

  • class_name − It is the name of the class preceded by a keyword class.

  • data_type − It is the type of data upon which the method works.

  • method_name − It is the name or tag of the method. This method can be invoked in the latter part of the program using the method name only.

  • Statements − These are the lines of code written within the method.

  • return − The value to be returned by the method is written here. It can contain any numeral or variable name.

Here is an illustration of the above syntax −

class Addition {
   data_type Add() {
      int a=3, int b=4;
      int sum = a + b;
      return sum;
  }
}

Steps

  • Start the program by declaring the class.

  • Within the class, define a method as per the requirement.

  • Within the method, declare and initialize the required variable.

  • Perform the required task.

  • Return the calculated value and close this method.

  • Define the main method and create an object of the class inside it.

  • Invoke the method created above through an object.

Example

The following program is written to demonstrate how a method is written without any parameters but 1 return type. A method named addition () containing no parameter but return type as double is created within a class named Add. The add () method gets invoked in the main method through the creation of an object of the created class and the value is displayed.

public class Add {
   // creation of method named addition without any parameter and 1 return value
   public double addition() {
      double sum = 567.98 + 34.76;
      // return value
      return sum;
   }
   public static void main(String[] args) {
      // creation of an object of class Add
      Add a = new Add();
      // calling the addition method 
      double sum = a.addition();
      System.out.println("Sum is: " + sum);
   }
}

Output

Sum is: 602.74

Example

The following program is written to demonstrate how a method is written without any parameters but 1 return type. a method named area_semicircle () containing no parameter but return type as double is created within a class named Area. The area_semicircle () method gets invoked in the main method through the creation of an object of the created class and the calculated value of the area is displayed on the screen.

public class Area {
   // creation of an area method without any parameter and 1 return value
   public double area_semicircle() {
      double r = 16.0;
      double area = (3.14 * r * r)/2.0;
      // return value
      return area;
   }
   public static void main(String[] args) {
      //creation of an object of class Add
      Area a = new Area();
      // calling the area_semicircle method 
      double area = a.area_semicircle();
      System.out.println("The area of semi circle is: " + area);
   }
}

Output

The area of semi circle is: 401.92

Conclusion

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

Updated on: 11-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements