Java Program To Create Methods

01. Example:

class createMethods {

    // Constructor method
    createMethods() {
        System.out.println("Constructor Method: It is called when an object of it's class is created. ");
    }

    // Main method where program execution begins
    public static void main(String[] args) {
        staticMethod();
        createMethods object = new createMethods();
        object.nonStaticMethod();
    }

    // Static Method
    static void staticMethod() {
        System.out.println("Static Method: It can be called without creating any object.");
    }

    // Non Static Method
    void nonStaticMethod() {
        System.out.println("Non-Static Method: It can not be called without creating object.");
    }
}

Output:

Static Method: It can be called without creating any object.
Constructor Method: It is called when an object of it's class is created. 
Non-Static Method: It can not be called without creating object.

Leave a comment