Java Methods | |||
![]() ![]() ![]() | |||
A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions. Why use methods? To reuse code: define the code once, and use it many times. Create a Method A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as
Example Explained
Call a Method To call a method in Java, write the method's name followed by two parentheses () and a semicolon; In the following example,
A method can also be called multiple times:
Parameters and Arguments Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. The following example has a method that takes a
Multiple Parameters You can have as many parameters as you like:
Return Values The
This example returns the sum of a method's two parameters:
You can also store the result in a variable (recommended, as it is easier to read and maintain):
A Method with If...Else It is common to use
Method Overloading With method overloading, multiple methods can have the same name with different parameters:
Consider the following example, which has two methods that add numbers of different type:
Instead of defining two methods that should do the same thing, it is better to overload one. In the example below, we overload the
Tags: Java Java Methods Java Parameters Method Overloading ![]() ![]() | |||
| |||
| |||
|
SIMILAR POSTSJava ScopeJava RecursionJava While Loop/Do While Loop/For Loop/For-Each Loop/Break/ContinueJava Classes and ObjectsJava Switch StatementsJava Short Hand If...Else (Ternary Operator)Java If ... ElseJava PackagesJava Inheritance (Subclass and Superclass)Java MathJava VariablesJava CommentsJava PolymorphismThe Print() MethodJava SyntaxJava Getting StartedJava Inner ClassesWhat is Java?Machine Learning Types and Programming LanguagesJava Tutorials associated with AP Computer Science ACreating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft WindowsJava Servlet ExampleDifference between Java and Javascript |
OTHER POSTS IN THE SAME CATEGORYJava Servlet ExampleHow do I replace content that based on the HTML UI TemplateCreating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft WindowsDataset of California FoodbanksJava Tutorials associated with AP Computer Science AJava Inner ClassesJava PolymorphismJava Inheritance (Subclass and Superclass)Java PackagesJava Abstract Classes and MethodsJava Classes and ObjectsJava RecursionJava ScopeJava ArraysJava While Loop/Do While Loop/For Loop/For-Each Loop/Break/ContinueJava Switch StatementsJava Short Hand If...Else (Ternary Operator)Java If ... ElseJava MathJava VariablesJava CommentsThe Print() MethodJava SyntaxJava Getting StartedWhat is Java? |