Method
A method is a block of statements that has a name and can be executed by calling (also called invoking) it from some other place in your program. Along with fields, methods are one of the two elements that are considered members of a class. (Constructors and initializers are not considered class members.)
Every program must have at least one method for the program to accomplish any work. And every program must have a method named main, which is the method first invoked when the program is run.
All methods — including the main method — must begin with a method declaration. Here’s the basic form of a method declaration, at least for the types of methods I talk about in this chapter:
visibility [static] return-type method-name (parameter-list)
{
statements...
}
The following list describes the method declaration piece by piece:
visibility
: The visibility of a method determines whether the method is available to other classes. The options are
• public: Allows any other class to access the method
• private: Hides the method from other classes
• protected: Lets subclasses use the method but hides the method from other classes
static
: This optional keyword declares that the method is a static method, which means that you can call it without first creating an instance of the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access