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

17 Jun, 2021
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. 
Area calculationCircumference calculation
Area calculation
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 classesimport java.util.*;// Importing Scanner class if// input is from user (optional)*/import java.util.Scanner; // Auxiliary 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 Classpublic 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);    }}
  
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 classesimport 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);    }}
  
Circumference of circle is : 43.96
 
varshagumber28
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.