0% completed
Methods in Java are blocks of code designed to perform specific tasks. They help in organizing code into reusable and manageable sections, making programs more modular and easier to maintain. By defining methods, you can execute the same code multiple times without rewriting it, enhancing code efficiency and readability.
accessModifier returnType methodName(parameters) { // Method body }
public
, private
, protected
). We will learn more about access modifiers in advanced Java course.int
, String
, void
if no value is returned).Calling a method in Java involves invoking the method by its name followed by parentheses. If the method requires parameters, you pass the necessary arguments inside the parentheses. Depending on the method's return type, you can also capture and use the returned value.
methodName(arguments);
If the method returns a value, you can store it in a variable:
returnType variableName = methodName(arguments);
returnType: The type of value the method returns.
variableName: The variable that will store the returned value.
Below is an example of a method that adds two integers and returns the result.
Explanation:
public static int addNumbers(int a, int b)
public
: Method is accessible from other classes.static
: Method belongs to the class, not instances of the class.int
: Method returns an integer value.addNumbers
: Name of the method.int a, int b
: Parameters the method accepts.int result = a + b;
Calculates the sum of a
and b
.return result;
Returns the sum to where the method was called.addNumbers(5, 10)
5
and 10
as arguments to the method.15
as the returned value and assigns it to sum
.Prints "Sum: 15" to the console.
Below is an example of a method that prints a greeting message without accepting any parameters or returning any value.
Explanation:
public static void greet()
void
: Indicates that the method does not return any value.greet
: Name of the method.System.out.println("Hello, welcome to Java programming!");
Prints the greeting message.greet();
greet
method, resulting in the message being printed.Prints "Hello, welcome to Java programming!" to the console.
Below is an example of a method that calculates the area of a rectangle given its length and width.
Explanation:
public static double calculateArea(double length, double width)
double
: Method returns a double value representing the area.calculateArea
: Name of the method.double length, double width
: Parameters representing the dimensions of the rectangle.double area = length * width;
Calculates the area by multiplying length and width.return area;
Returns the calculated area.calculateArea(5.5, 3.2)
5.5
and 3.2
as arguments.17.6
as the returned value and assigns it to area
.Prints "Area of the rectangle: 17.6" to the console.
Defining methods is a fundamental concept in Java that promotes code organization, reusability, and clarity. By mastering method creation and usage, you can build more efficient and maintainable Java applications.
.....
.....
.....