Table of contents
There are 2 types of methods:
Static methods
Non-static methods (Dynamic)
In this article, we will focus on static methods.
Methods are simply functions. They outline how specific behaviors of a class
should be performed. They are used to access the fields of a class
and use that data. Generally, methods should be public but they can also be made private if they should not be accessed.
Example:
class Car {
// Public method to print car manufacturer
public void printMake(String manufacturer) {
System.out.println("Manufacturer: " + manufacturer);
}
}
class Main {
public static void main(String args[]) {
Car car1 = new Car();
car1.printMake("Honda"); // calling public method
}
}
Method parameters make it possible to pass values to the method and return type makes it possible to get the value from the method. The actual values passed to the function are called arguments. Method parameters must also have a type.
The parameters are declared inside the parentheses after the method name while the return
type is declared before method name eg int
, char
etc. The return type is one Java's data types (shown below)
For methods that define a return type, the return statement must be immediately followed by the return value. If not, it will result in errors. Every method must have its return type declared, even if the method has no return value.
All these requirements enforce Java's robustness and strong security features. This ensures that the code you write actually does what it's meant to do.
Example:
// public method with one parameter (topSpeed) of type of int. The method has a return type of int
public int printTopSpeed(int topSpeed) {
// ...
return topSpeed + 50; // return statement
}
Getters and setters
A get method retrieves the value of a particular data field, whereas a set method sets its value.
Whenever you declare/define member variables, it is good practice to include getters and setters. Getters and setters strictly define how to retrieve and use the variables thus adding a layer of security to your code.
class Car {
private int topSpeed; // member variable
// Setter method to set the maximum speed of the car
public void setTopSpeed(int x) {
topSpeed = x;
}
// Getter method to get the top speed of the car
public int getTopSpeed() {
return speed;
}
}
class CarDemo {
public static void main(String args[]) {
Car car1 = new Car();
car1.setTopSpeed(360); // calling the setter method
System.out.println(car1.getTopSpeed()); // calling the getter method
}
}
Method overloading
Method overloading refers to a situation where you have a number of methods with the same name but different arguments. Based on the arguments, the method will behave differently. Whenever a method is called, the compiler will know which method you are referring to because they all expect different argument types despite having the same name.
Using different parameter names will not work either for the same reason.
Example:
class Addition {
public int addition(int x, int y) {
return x + y;
}
// Overloading the function to handle three arguments
public int addition(int x, int y, int z) {
return x + y + z;
}
// Overloading the function to handle double
public double addition(double x, double y){
return x + y;
}
}
class AdditionDemo {
public static void main(String args[]) {
Addition add = new Addition();
int x = 50;
int y = 60;
int z = 105;
double a = 32;
double b = 54;
System.out.println(add.addition(x, y));
System.out.println(add.addition(x, y, z));
System.out.println(add.addition(a, b));
}
}
The code will behave differently depending on the arguments/inputs.
Note: Overloading doesn't depend on the return type. Overloading only depends on the arguments. Having different return types doesn't make the function overloaded because the compiler won't know how to differentiate between return types but will be able to do so with different inputs.
As you can see, method overloading makes the code you write simple, clean, and easy to understand. Overloading also allows you to implement an important OOP principle: polymorphism.
The main
method
The main
method in Java is used to execute the program. Without it, you cannot run a Java program.
The main
method is declared as public static void main (String [] args)
. The void
indicates that main
does not return a value. In C or C++, the main method traditionally returns an integer, indicating to the operating system whether the program ran successfully or not.
Conclusion
In conclusion, we have covered the most important concepts when it comes to methods in Java. As we dive deeper into OOP, we will discover other unique implementations of methods specifically dynamic methods. Thank you for reading thus far! ๐ Happy coding! ๐