Search⌘ K

Methods

Explore how methods in Java act as interfaces to manipulate data within classes. Learn to declare methods with parameters and return types, use getters and setters for data access, and implement method overloading to simplify code and support polymorphism.

The purpose of methods

Methods act as an interface between a program and the data fields of a class in the program.

These methods can either alter the content of the data fields or use their values to perform a certain computation. All the useful methods should be public, although, some methods which do not need to be accessed from the outside could be kept private.

Definition and declaration

A method is a group of statements that performs some operations and may or may not return a result.

Here is an example of a method in Java: ...