- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are methods in Java?
A Java method is a collection of statements that are grouped together to perform an operation.
When you call the System.out.println()method, for example, the system actually executes several statements in order to display a message on the console.
Syntax
modifier returnType nameOfMethod (Parameter List) { // method body }
The syntax shown above includes −
Modifier − It defines the access type of the method and it is optional to use.
ReturnType − Method may return a value.
NameOfMethod − This is the method name. The method signature consists of the method name and the parameter list.
Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
Method body − The method body defines what the method does with the statements.
Example
Following code demonstrates how to write a method in Java. This method takes two parameters num1 and num2 and returns the maximum between the two −
public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; }
- Related Articles
- What are defender methods or virtual methods in Java?
- What are generic methods in Java?
- what are abstract methods in Java?
- What are vararg methods in Java?
- What are Default Methods in Java 8?
- What are Class/Static methods in Java?
- What are native methods in Java and where are they used?
- Are static methods inherited in Java?
- What are Java methods equivalent to C# virtual functions?
- What are the differences between compareTo() and compare() methods in Java?
- What are the conditions for Collection factory methods in Java 9?
- What are the modifiers allowed for methods in an Interface in java?
- What are native methods in Java and where should we use them?
- What are the new methods added to Process API in Java 9?
- What are new methods added to the String class in Java 9?
