KnowledgeBoat Logo
|

Computer Applications

How do you define and invoke a method?

User Defined Methods

48 Likes

Answer

The syntax of a method definition is:

[access-modifier] type method-name (parameter-list)
{
    method-body;
}

To invoke a method, write the name of the method and specify the value of arguments of its parameter list. Below example shows the definition and invocation of a method:

public class DisplayMessageDemo {

    public void DisplayMessage() {
        System.out.println("Hello World!");
    }

    public void MyMessage() {
        DisplayMessage();
    }
}

Answered By

29 Likes


Related Questions