Java: Method
When we want to execute specific function repeatedly, we can create a method a call it on our main function. Method is created to make sure that we can reuse our program and make it flexible to combine it with another method to get the output that we need.
public class Main {
public static void main(String[] args){
System.out.println("This is a method to print string');
}
}Get Aldyputu’s stories in your inbox
Join Medium for free to get updates from this writer.
Above code is println statement that is method to print string on our computer. Lets see below code to understand how to create and use our customize method.
public class Main {
static void sayHelloWorld() { //static can only called static
System.out.println("Hello World!"); //this is our method
}
public static void main(String[] args){
sayHelloWorld(); //this is how we call the method
}
}public void methodName() is the way the initialize the method and then on the next line we define the function that needed in our code. For above case we want to print “Hello World!” when the function is called. This is when we realize that method is reusable and lets deep dive into it.



