Comp 110 Program's Starting Point: main Method

The Main Method - What it is

The main method is the first method that runs when you click "run" in Eclipse. Every Java program begins running from a main method. Take the following class as an example: 

public class Test { 
    public void printFifteen() {
        for(int i = 0; i < 15; i++){
            System.out.println(i);
        }
    }
}

What happens when you click the "run" method? Well, nothing happens. Why is this? It is because there is no main method! Let us create a main method in our class (every main method must be defined in a class). 

The main method signature is: 

public static void main(String [] args)

Below we added the main method. Now our code will compile and run, correctly printing out 0 to 14. Notice that we added a static keyword in print of printFifteen(). The static keyword is needed because we are calling a method in the same class as the main method.

public class Test { 
    public static void main(String [] args) {
        printFifteen()
    }
    public static void printFifteen() {
        for(int i = 0; i < 15; i++){
            System.out.println(i);
        }
    }
}