![]() |
VOOZH | about |
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.
Hello, Geeks!
Explanation:
π Method Body in JavareturnType methodName(parameters) {
// method body
return value; // optional (only if returnType is not void)
}
Key Components of a Method Declaration:
void if no return Breaking code into separate methods helps improve readability, reusability, and maintainability
Java is an object-oriented and stack-based programming language where methods play a key role in controlling the program's execution flow. When a method is called, Java uses an internal structure known as the call stack to manage execution, variables, and return addresses.
The call stack is a data structure used by the program during runtime to manage method calls and local variables. It operates in a Last-In-First-Out (LIFO) manner, meaning the last method called is the first one to complete and exit.
When a method is called:
Java automatically manages the call stack using the Java Virtual Machine (JVM).
Let's use an image to understand how the method call stack works
Example: Letβs understand how the above image works with the help of a Java code example
In Method C In Method B In Method A In Method D
Predefined methods are the method that is already defined in the Java class libraries. It is also known as the standard library method or built-in method.
For example, random() method which is present in the Math class and we can call it using the ClassName.methodName() as shown in the below example.
The method written by the user or programmer is known as a user-defined method. These methods are modified according to the requirement.
Example:
There are two ways to create a method in Java:
1. Instance Method: Access the instance data using the object name. Declared inside a class.
Example:
2. Static Method: Access the static data using class name. Declared inside class with static keyword.
Example:
It consists of the method name and a parameter list.
Note: The return type and exceptions are not considered as part of it.
Method Signature of the above function:
max(int x, int y) Number of parameters is 2, Type of parameter is int.
In Java language method name is typically a single word that should be a verb in lowercase or a multi-word, that begins with a verb in lowercase followed by an adjective, noun. After the first word, the first letter of each word should be capitalized.
Rules to Name a Method:
Method calling in Java means invoking a method to execute the code it contains. It transfers control to the process, runs its logic, and then returns to the calling point after execution.
User-defined methods are blocks of code written by the programmer. To execute a user-defined method, we first create an object of the class (if the method is non-static) and then call the method using that object.
Example:
This is a user-defined method.
Explanation: In the above code a class Geeks with a method hello() that displays a message. In the main() method, we create an object of the class and use it to call the hello() method.
Abstract methods have no body and must be overridden in a subclass. They are called using an object of the subclass.
Example 2: Calling Methods in Different Ways
GeeksforGeeks
Explanation: In the above code an abstract class GeeksHelp with an abstract method check(), which is implemented in the subclass Geeks. In the main() method, an object of Geeks is created to call the implemented check() method.
Java provides many built-in methods via the Java Standard Library, like hashCode().
1510467688
Explanation: In the above code an object of the Geeks class and calls the predefined hashCode() method.
hashCode() returns an integer hash code for the object. This value is used in hash-based collections like HashMap and HashSet. It is not guaranteed to be unique or represent the memory address.
Static methods belong to the class, not the object. They can be called without creating an object.
Hello
Explanation: we call a static method hello() from the test class without creating an instance of the class. The method prints "Hello" when invoked from the main method.