Java-Method Returns

Method Returns

A method may be instructed to return a value after its execution is over. A special statement namely the return shall be used for this purpose.

Program

GFHFGH

After a1.areaVolume( ) executes, the value of a1.areaVolume( ) is 187500. There are two important things to understand about returning values:

1. The type of data returned by a method must be compatible with the return type specified by the method. For example, if the return type of some method is boolean, you could not return an integer.
2. The variable receiving the value returned by a method must also be compatible with the return type specified for the method.

Program Source

class Area{
 
    int length;
    int hight;
    int width;
 
    int areaVolume()
    {
        return length*hight*width;
    }
}

public class Javaapp {

    public static void main(String[] args) {
     
        Area a1 = new Area();
        a1.length = 100;
        a1.hight  = 25;
        a1.width  = 75;
        System.out.println("Area volume is : "+a1.areaVolume());
    }
}

Leave a Comment