Java Methods
Java Tutorial » Java Methods
A method is a block of code that performs a specific task.
Methods are used to perform certain actions, known as functions.
Suppose we have to create a program to draw a square and color it. You can create two methods to solve this problem:
- the method of drawing the square,
- square coloring method
Syntax:
returnType my_method_Name() {
// method body
}
// method body
}
Create a Method, example
A method is declared within a class and defined with name of the method, followed by "()" (parentheses without quotes)
Java programs provides some pre-defined methods, like System.out.println(), but you can create your own methods to perform actions
Example: method inside Main
public class Main {
static void my_Method() {
// code java to be executed
} }
static void my_Method() {
// code java to be executed
} }
Calling Method example
In the next example, we have declared a method named multiplication() and to use this method, we need to call it.
Example:
The result of multiplying the two numbers is: 216
public class Main {
// create a method
public int multiplication(int x, int y) {
int multiplication = x * y;
// return value
return multiplication;
}
public static void main(String[] args) {
int my_num1 = 12;
int my_num2 = 18;
// create an object of Main
Main obj = new Main();
// calling method
int result = obj.multiplication(my_num1, my_num2);
System.out.println("The result of multiplying the two numbers is: " + result);
}
}
Output: // create a method
public int multiplication(int x, int y) {
int multiplication = x * y;
// return value
return multiplication;
}
public static void main(String[] args) {
int my_num1 = 12;
int my_num2 = 18;
// create an object of Main
Main obj = new Main();
// calling method
int result = obj.multiplication(my_num1, my_num2);
System.out.println("The result of multiplying the two numbers is: " + result);
}
}
The result of multiplying the two numbers is: 216
Java Method Return Type
A Java method may or may not return a value to the function call. We use the return statement to return any value.
Example:
int add_Numbers() {
...
return sum;
}
...
return sum;
}
Here, we are returning the variable sum. Since the return type of the function is int. The sum variable should be of int type. Otherwise, it will generate an error.
Example:
Output: Squared value of 10 is: 100
class Main {
// create a method
public static int square(int num) {
// return statement
return num * num;
} public static void main(String[] args) {
int result;
// call the method
// store returned value to result
result = square(10);
System.out.println("Squared value of 10 is: " + result);
}
}
// create a method
public static int square(int num) {
// return statement
return num * num;
} public static void main(String[] args) {
int result;
// call the method
// store returned value to result
result = square(10);
System.out.println("Squared value of 10 is: " + result);
}
}
Output: Squared value of 10 is: 100
java methods examples, exercises, string, in interface, and parameters, witv same name, types, exercises pdf, list, for financial engineering pdf
Java Methods - java
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.