Methods in Java.

Overview.

In Java, a method is a block of code that performs a specific task and can be reused throughout a program. Methods are defined with the keyword "void" or a return type and have a name, a set of parameters, and a body. Methods in Java allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is different from languages like C, C++, and Python.

1. A method is like function i.e. used to expose behavior of an object.
2. it is a set of codes that perform a particular task.

Example:

Here is an simple example of method in java;

public void printhello() {

system.out.println("Hello World");

{

This method is called "printhello" and it doesn't take any parameters, it just prints "Hello World" when called.

Java also supports method overloading, which allows for multiple methods with the same name but different parameter lists to be defined in the same class. This allows for methods to be reused with different input types.

Java also has a built-in set of methods that are available to all objects, known as Object class methods. These include methods like toString(), equals(), and hashCode().

Advantage of Method

  • Code Reusability

  • Code Optimization

    Method Declaration

    In general, method declarations has six components :

    1. Modifier: It defines the access type of the method i.e. from where it can be accessed in your application. In Java, there 4 types of access specifiers.

    • public: It is accessible in all classes in your application.

    • protected: It is accessible within the class in which it is defined and in its subclass/es

    • private: It is accessible only within the class in which it is defined.

    • default: It is declared/defined without using any modifier. It is accessible within the same class and package within which its class is defined.

**2\. The return type:** The data type of the value returned by the method or void if does not return a value.

**3\. Method Name:** the rules for field names apply to method names as well, but the convention is a little different.

**4\. Parameter list:** Comma-separated list of the input parameters is defined, preceded with their data type, within the enclosed parenthesis. If there are no parameters, you must use empty parentheses ().

**5\. Exception list:** The exceptions you expect by the method can throw, you can specify these exception(s).

**6\. Method body:** it is enclosed between braces. The code you need to be executed to perform your intended operations.

Types of Methods in Java

There are two types of methods in Java:

1. Predefined Method: In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method. We can directly use these methods just by calling them in the program at any point.

2. User-defined Method: The method written by the user or programmer is known as a user-defined method. These methods are modified according to the requirement.

Memory Allocation for Methods Calls.

Methods calls are implemented through a stack. Whenever a method is called a stack frame is created within the stack area and after that, the arguments passed to and the local variables and value to be returned by this called method are stored in this stack frame and when execution of the called method is finished, the allocated stack frame would be deleted. There is a stack pointer register that tracks the top of the stack which is adjusted accordingly.