Java: Calling methods

In Java, a method is just like a function in C.

The method that we are creating here will simply print out a text message. To invoke (call) it from the main method an instance of the MyApp class must first be created.

The dot operator is then used after the instance’s name in order to access its members, which includes the PrintOut method.

package javaapplication15;

public class JavaApplication15 {

     static class MyApp {

        void  PrintOut() {
            System.out.println("Hello");
        }
    }

    public static void main(String[] args) {
        MyApp m = new MyApp();
        m.PrintOut();
    }
}

method

With the declaration

MyApp m = new MyApp();

m is now an instance of the class MyApp.  We can now use m to access a method of the class PrintOut as in

m.PrintOut();
Series Navigation<< Java: Do while loop
Java: Method parameters or arguments >>