Posts

Showing posts with the label method. method overloading

Method in Java

Image
Methods appear inside class bodies. They contain local variable declarations and other Java statements that are executed when the method is invoked. Methods may return a value to the caller. They always specify a return type, which can be a primitive type, a reference type, or the type void , which indicates no returned value. Methods may take arguments, which are values supplied by the caller of the method. Lets see example: Program class multiply { int num1; int num2; int mul(int x,int y) { int result; num1 = x; num2 = y; result = num1*num2; return result; } } public class Main { public static void main (String[] args) { multiply m = new multiply(); System.out.println("multiplication of 3 and 5 is " + m.mul(3,5)); } } Run Output multiplication of 3 and 5 is 15 Explanation In this example, the class multiply defines a method, mul() , that takes as argu...