Last Updated: 18 November, 2023
A method is a set of instructions. These instructions are enclosed within a pair of curly braces and designed to perform a specific task. Every method has a name, and it may take some parameters and return some values after execution.
Methods are used to organize and reuse code, making it easier to write and maintain complex programs.
In Java, all the methods must be declared inside a class and are executed only when they are called. A method can be called one or more times based on the requirement. A method can be accessed outside the class based on the access modifier.
📝 A method is also known as a function.
Modularization: Methods are used to break down complex programs into smaller, more manageable units.
Reusability: Methods can be called repeatedly throughout the program wherever required; reusability saves time and effort.
Readability: Methods enhance code readability by facilitating the grouping of logically related instructions.
Data Encapsulation: Methods can hide implementation details and expose only necessary data to other parts of the program.
In the method declaration, we provide complete information about the method attributes, such as the method name, return type, access modifier, etc.
A Java method has a total of six components, as described below. It is also known as the method header. Let's discuss them one-by-one:
1. Modifier: It defines the access type or access modifier of the method. We can define four types of access modifiers with the method: public, private, protected, and default.
Public: The public access modifier allows unrestricted access to variables, methods, constructors, or nested classes from anywhere within the program.
Private: It provides strong access restriction over the method; if a method is declared with a private modifier, then that method is accessible only in the classes in which it is defined.
Protected: With the protected modifier, the method is accessible within the same package or subclasses in a different package.
Default: If we don't define any modifier, then that method has a default modifier, and that method is accessible only in the same package.
2. Return Type: After execution, a method may return a value; the value may be a single or multiple of any type, such as primitive or object. If a method does not return any value, then the return type should be void.
3. Method Name: A method name specifies a unique name to identify it. The method name should correspond to the functionality that the method is performing. A method name should start with a lower-case letter in Java.
4. Parameters: The data it may receive and the data values should be a comma-separated list enclosed in a pair of parentheses. If a method does not have a parameter, then use empty parentheses (). If we call a parameterized method, then we must pass the valid parameter values at the time of method calls.
5. Exceptions: According to the method functionality, we write the expected exception name that the method may throw.
6. Method body: It contains a set of instructions that we want to perform through this method; these instructions are enclosed within the pair of curly braces. All these instructions are executed one by one when we call the method.
A pictorial representation of Method Declaration in Java.
In Java, there are two types of methods:
✅ Predefined Method: These are built-in methods in Java defined in the Java class libraries. We can use these methods according to the requirements. It is also known as the standard library method or built-in method.
✅ User-defined Methods: A method written by a user to perform some specific task is known as a user-defined method.
A method is created inside a class; every method has a name and is responsible for performing some specific task.
Syntax for creating methods in Java
A method can be called inside the class or outside the class, depending on the requirement. Let's see how to call a method in Java.
Calling a method outside the class:
Instance methods: Using class instance + dot operator + method name
Static methods: Using class name + dot operator + method name
Calling a method inside the class:
In the case of calling a method inside the same class, we can directly call that method; we don't need an instance or class name. All the private methods are called inside the class only.
While calling a parameterized method, we need to pass the proper parameters, and if the method has a return type, we should provide the proper receiver.
Example: Method calling in Java
Output
It is a Static method
It is an Instance method
It is a private method
Total sum: 60
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is method Signature?
Ans. A method signature is a combination of the method name and method parameters. The return type and exceptions are not considered in it.
For example,
What is the difference between an instance method and a static method in Java?
Ans. In Java, instance methods and static methods are two different types of methods that serve distinct purposes. Understanding their key differences is crucial for designing and implementing effective object-oriented programs.
Instance Method | Static Method |
---|---|
An instance method is a normal Java method that is created without the static keyword.
public void display(){} |
A static method is always created with a static keyword.
public static void display(){} |
Memory is allocated multiple times whenever a method is called. | Memory is allocated only once at the time of class loading. |
It is specific to an instance, so it is known as an instance method. | It is specific to a class and common for all instances, so it is known as a class method. |
These methods always access with object reference.
Syntax: object.methodname(); |
These properties are accessed with class reference.
Syntax: className.methodname(); |
When to use static methods and when to use instance methods in Java?
Ans. When to Use Each:
Use instance methods for operations that are specific to an individual object and require access to its state.
Use static methods for operations that are common to all objects of the class and do not depend on the state of any individual object.
How do you decide whether a variable or method should be an instance or static?
Ans. Use instance variables when: Every variable has a different value for a different object. E.g., name of student, roll number, etc. Use static variables when: The value of the variable is independent of the objects (not unique for each object).