Problem scenario
You have a public static void main (String[] args)
portion of your Java program. You want to have a subroutine such as a function outside of this section. How do you code this?
Solution
In Java methods are components of an object that perform an action on the object themselves. We therefore use the term "method" here and not "function". Here is an example of a method inside a class that runs from the public static void main(String[] args)
section of code:
public class ContClass {
static void contMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
contMethod();
}
}
Call the above file ContClass.java. Run these commands:
javac ContClass.java
java ContClass