Java-Methods
A method in Java is a structured block of code that is designed to carry out a particular operation whenever it is invoked within a program. Methods are fundamental to defining the behavior of objects in Java, allowing you to encapsulate specific actions or calculations within named units of code. Each method can accept data through parameters, execute a set of instructions, and optionally return a result to the caller. By organizing code into methods, you not only enhance the reusability and clarity of your application but also make it easier to maintain and debug. Methods enable you to divide complex tasks into smaller, logical components, making your programs more modular, readable, and manageable. In Java, methods must be declared within a class, and they can be called multiple times from different parts of your code, promoting efficient and organized software development.
Syntax:
Example:
Output:
Output:
Output:
Output:
Output:
Example:
Output:
Syntax:
access_modifier return_type methodName(parameters) {// Method body}
Example:
public void helloWorld(int a) { // code}
Parameters in Methods
A parameter is a variable listed inside the parentheses in the method definition. It acts as a placeholder for the value you want to pass to the method.
Example:
A parameter is a variable listed inside the parentheses in the method definition. It acts as a placeholder for the value you want to pass to the method.
Example:
public class Main { public static void main(String[] args) { String name = "Quipo House"; message(name); // 'name' is the argument } static void message(String name) { // 'name' is the parameter System.out.println(name); }}
Output:
Quipo House
Arguments
An argument is the actual value that gets passed to the method’s parameter when the method is called.
Example:
An argument is the actual value that gets passed to the method’s parameter when the method is called.
Example:
public class Main { public static void main(String[] args) { message("HELLO QUIPO HOUSE"); // "HELLO QUIPO HOUSE" is the argument } static void message(String message) { System.out.println(message); }}
Output:
HELLO QUIPO HOUSE
Types of Methods in Java
1. Predefined Methods (Built-in Methods)
These are methods already provided by Java libraries. You use them without having to define their logic.
Example:
1. Predefined Methods (Built-in Methods)
These are methods already provided by Java libraries. You use them without having to define their logic.
Example:
public class Main { public static void main(String[] args) { String name = "QUIPO HOUSE"; System.out.println(name.toLowerCase()); // toLowerCase() is a predefined method }}
Other examples include Math.max(), Math.min(), System.out.println(), etc.
2. User-Defined Methods
These are methods written by the programmer to perform specific tasks.
Example:
These are methods written by the programmer to perform specific tasks.
Example:
public class Main { public static void main(String[] args) { String name = "Quipo House"; message(name); }
static void message(String name) { System.out.println(name); }}
Output:
Quipo House
3. Static Methods
A static method belongs to the class rather than to any specific object. You can call a static method directly using the class name or from another static method.
Example:
A static method belongs to the class rather than to any specific object. You can call a static method directly using the class name or from another static method.
Example:
public class Main { public static void main(String[] args) { String name = "Quipo House"; message(name); // Direct call Main.message(name); // Call using class name Main m = new Main(); m.message(name); // Call using object }
static void message(String name) { System.out.println(name); }}
Output:
Quipo House
Quipo House
Quipo House
4. Instance Methods
Also known as non-static methods, these require you to create an object of the class to call them.
Example:
Also known as non-static methods, these require you to create an object of the class to call them.
Example:
public class Main { public static void main(String[] args) { InstanceMethod obj = new InstanceMethod(); obj.displayMessage("HELLO QUIPO HOUSE"); }}
class InstanceMethod { public void displayMessage(String message) { System.out.println(message); }}
Output:
HELLO QUIPO HOUSE
5. Abstract Methods
An abstract method is declared without a body and is marked with the abstract keyword. It must be implemented by subclasses of the abstract class.
Syntax:
An abstract method is declared without a body and is marked with the abstract keyword. It must be implemented by subclasses of the abstract class.
Syntax:
abstract void methodName();
Example:
abstract class Message { abstract void greet();
void displayName() { System.out.print("QUIPO HOUSE"); }}
class Greet extends Message { void greet() { System.out.print("HELLO "); }}
public class Main { public static void main(String[] args) { Greet obj = new Greet(); obj.greet(); obj.displayName(); }}
Output:
HELLO QUIPO HOUSE
Key Points
- Methods enhance code reusability and maintainability.
- Static methods belong to the class and can be called without creating an object.
- Instance methods require an object to be called.
- Abstract methods provide a blueprint for subclasses, enforcing implementation.
Tips: Use methods to keep your code modular, readable, and easy to maintain. Choose the appropriate type of method based on your use case and design requirements.