Modularity in Java with methods
When creating a program, sometimes we create the same code repeatedly in different places, for example, code to prints greetings at different times of course this will really waste time and lines of code. Fortunately, there are ways to make this problem easier and more efficient. In some programming languages, they have a modular programming concept, one of which is Java. But, what is modular programming?
What is modular programming?
Modular programming is the concept of breaking program into several pieces of code / modules that can be used repeatedly. In Java, a piece of code / module is called method. A method is a block of code that can execute specific commands as written within it. Methods consist of two parts: parameters and actions. Parameters contain variables that can be used within the method, derived from the values entered during the call. Meanwhile, actions are a all the commands contained within the method. Example:
void greeting(String time){ <= parameters
System.out.println("Good " + time); <= actions
}
public static void main(String[] args){
greeting("morning"); <= the time parameter is filled with "morning"
greeting("night"); <= the time parameter is filled with "night"
}Types of method in Java
- Predefined method
Predefined methods are the method that is already defined in the Java class library. It is also known as the standard library method or built-in method. Example of predefined method such as:
Math.random(); // returns random value
Math.pi(); // returns pi value
text.toLowerCase(); // change string to lower-case - User-defined method
The method written by user or programmer is known as user-defined method. These methods are modified according to the requirement. There are two types of user-defined method: method with return value and method without return value. - Method with return value, it means the method will return a value to its caller. The main characteristic of this method that it has the return keyword at the end of its code to return the result value. Example:
void calcExponent(int basis, exponen){
int result = 1;
for(int i = 0; i <= exponen; i++){
result = result * basis;
}
return result;
}
public static void main(String[] args){
int eksponenTwo = calcExponent(2,3); // the value of 2^3 is 8
}- Method without return value, it means the method not return any value to its caller. This method will only execute code contained within it. Example:
void printMessage(boolean success){
if(success){
System.out.print("Success");
} else {
System.out.print("Failed");
}
}
public static void main(String[] args){
int packets = 5;
printMessage(packets == 8);
}Conclusion
In programming, methods or modules are a way to break down complex code into simpler, reusable pieces, which can improve maintainability, readability, and performance. There are several types of methods: predefined methods provided in Java libraries and user-defined methods created by users. Each type of method has its own advantages, so choose one that best suits your needs.
Get Farel Alghazali’s stories in your inbox
Join Medium for free to get updates from this writer.
That’s all for this article. Thank you for reading, and feel free to leave any questions in the comments section below.







