Skip to content
Related Articles

Related Articles

Java Program to Illustrate a Method without Parameters But with Return Type
  • Last Updated : 08 Dec, 2020

The task is to illustrate a method that does not contain any parameter but should return some value. Methods are used to break a complete program into a number of modules that can be executed one by one. Considering any random shape to illustrate: Circle

Example: First a class named ‘Circle’ is supposed to be created, then with the help of this class, it can calculate the area and circumference of a circle. 

  1. Area calculation
  2. Circumference calculation

Inside the class Circle, we will declare a variable called radius to take the value of the radius from the user so that by using this value of radius we will be able to calculate the area and circumference of a circle. Now, for calculating the area and circumference, we need to create two separate methods called area() and circumference(), which does not take any value as a parameter but will definitely return some value, and the value which this method will return are the final values of area and circumference of the circle. The methods will return the values after calculating it with the help of the values of variables. The most important thing to calculate the area and circumference, and to use these methods are first to need is to call those methods which are known as calling a function, for this we have to create an object of Circle class because in the main method, and we want to access them, methods of another class hence we need to make the object of that class which contains our methods area() and circumference() and by using the object of the class Circle, we will easily call the methods and those methods will return some value, and we will display it by using our predefined function.

Syntax:

 class name
    {
        datatype MethodName()
        {
            statements....
            ..
            ..
            return value;
        }
    }

Implementation:



Example 1: Area calculation

Java




// Java Program to Illustrate a Method
// without Parameters but with Return Type
  
// Importing generic java classes
import java.util.*;
// Importing Scanner class if
// input is from user (optional)*/
import java.util.Scanner;
  
// Auxilary Class (Sample class)
// class which contains the method area()
class Circle {
  
    // initializing the variable radius
    double radius;
  
    // functions calculating area
    double area()
    {
        double r = 7;
  
        // Area of circle will be calculate and
        // will store into the variable ar
        double ar = 3.14 * r * r;
  
        // Method returning the area
        return ar;
    }
}
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Object of circle is created
        Circle c = new Circle();
        
        // Big type variable to hold area value
        double area;
  
        // Area method is called and
        // will store the value in variable area
        area = c.area();
  
        // Printing area of circle
        System.out.println("Area of circle is : " + area);
    }
}
Output
Area of circle is : 153.86

Example 2: Circumference calculation

Java




// Java Program to Illustrate a Method
// without Parameters but with Return Type
  
// Importing java generic classes
import java.util.*;
  
class Circle {
    
    double radius;
    
    // method which does not contain
    // parameter but will return
    // the circumference of circle
    double circumference()
    {
         
        double r = 7;
        double circum = 2 * 3.14 * r;
        
        // Method returning the circumference of circle
        return circum;
    }
}
public class GFG {
    
    public static void main(String args[])
    {
        // the object of circle is created to call the
        // method circumference()
        Circle c = new Circle();
        double circum;
        
        // the value returned from method will store into
        // the variable circum
        circum = c.circumference();
        System.out.println("Circumference of circle is : "
                           + circum);
    }
}
Output
Circumference of circle is : 43.96

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :