Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Method Overloading

class MethodOverloading {
    public static void main(String[] args) {
        System.out.println("main");
        MethodOverloading.hello();
        MethodOverloading.hello("Brave New World");
        MethodOverloading.hello(42);
    }
    public static void hello() {
       System.out.println("hello");
    }
    public static void hello(String message) {
       System.out.println("hello with message: " + message);
    }
    public static void hello(int answer) {
        System.out.println("the answer is: " + answer);
     }
}

$ javac MethodOverloading.java
$ java MethodOverloading
main
hello
hello with message: Brave New World
the answer is: 42