<p>In this article, you’ll learn about Java methods, how to define a Java method and use them in your program with the help of examples.</p>
<h3><span style="color: #000080;"><strong>Methods</strong></span></h3>
<p>A Java <strong><span style="color: #008000;">method</span></strong> is a collection of statements that are grouped together to perform an operation.</p>
<p>Methods allow us to reuse the code without retyping the code i.e. you can write a method once, and use it multiple times. It also makes code more readable and easier to debug.</p>
<p>As Java is an <span style="color: #008000;"><strong>Object Oriented Programming</strong></span> language, every method must be a part of some class which defines the behavior of a class.</p>
<h3><span style="color: #000080;"><strong>Types of Methods</strong></span></h3>
<p>In Java, depending on whether a method is defined by the user or available in the standard library, methods are of two types:</p>
<ul>
<li>Standard Library Methods</li>
<li>User-defined Methods</li>
</ul>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Standard Library Methods</strong></span></h4>
<p>The Standard library methods are built-in methods in Java that are ready to use.</p>
<p>Examples of Standard library methods are:</p>
<ul>
<li><span style="color: #0000ff;"><strong>print()</strong></span>: method comes under<span style="color: #008000;"><strong> java.io.PrintSteam</strong></span> , prints the string written within the quotation.</li>
<li><strong><span style="color: #0000ff;">sqrt()</span></strong>: method of <strong><span style="color: #008000;">Math</span></strong> class that returns the square root of a specific number.</li>
</ul>
<h4></h4>
<h4><strong><span style="color: #0000ff;">For example:</span></strong></h4>
<pre>public class Number {

 public static void main(String[] args) {
 <span style="color: #008000;"><strong>//max() of Math class will print maximum number out of 16,8</strong></span>
 System.out.print("Maximum number is: " + Math.max(16,8));

 }</pre>
<h4><strong><span style="color: #0000ff;">Output:</span></strong></h4>
<pre>Maximum number is: 16</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>User-defined Methods</strong></span></h4>
<p>You can also define your own methods inside a class and such methods are called user-defined methods.</p>
<p>To work with user-defined methods, firstly we have to define a method then call it or use it.</p>
<h3><span style="color: #000080;"><strong>Defining a method</strong></span></h3>
<p>Before you can use a method (call a method), you need to define it.</p>
<h4><strong><span style="color: #0000ff;">Syntax:</span></strong></h4>
<p>The syntax of defining a method:</p>
<pre>modifier returnType nameOfMethod(Parameter list) {
 <strong><span style="color: #008000;"> //Method body</span></strong>
}</pre>
<p>More generally, method declarations have the following components, in order:</p>
<p><span style="color: #0000ff;"><strong>modifier</strong></span>-: defines access type of the method i.e. whether this method will be called or will be visible to any other class or package or not. In Java, access modifiers are of four types:</p>
<ul>
<li><strong><span style="color: #0000ff;">public</span></strong>: accessible within the class, within the package and outside the package.</li>
<li><strong><span style="color: #0000ff;">protected</span></strong>: accessible within the class, within the package in which this class is defined and also outside the package but through inheritance only.</li>
<li><strong><span style="color: #0000ff;">private</span></strong>: accessible only within the class in which it is defined.</li>
<li><strong><span style="color: #0000ff;">default</span><span style="color: #008000;"> (defined without using any modifier)</span></strong>: accessible within the class and within the package only.</li>
</ul>
<p><span style="color: #0000ff;"><strong>returnType</strong></span>: defines the data type of the value returned by the method or <span style="color: #008000;"><strong>void</strong></span> if a method does not return a value.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p><span style="color: #0000ff;"><strong>nameOfMethod</strong></span>: You can give any name to a method. But it&#8217;s more conventional to name it upon the task it performs. As per the naming convention, a method should start with lower case and if it contains more than one word then every word starting should be of upper case.For example: calculateAddition, displayStudentDetails etc.</p>
<p><strong><span style="color: #0000ff;">Parameter list</span></strong>: Parameters are the values passed to a method. You can pass any no of arguments to a method. If there are no parameters, you must use empty parentheses ().</p>
<p><strong><span style="color: #0000ff;">Method body</span></strong> : The code inside curly braces { } is the body of the method. It defines what the method actually does, how the parameters are manipulated with statements and what values are returned.</p>
<h4><span style="color: #0000ff;"><strong>Example:</strong></span></h4>
<pre>public int max(int x, int y) {
 if (x >; y) 
 return x;
 else 
 return y; 
}</pre>
<h3><span style="color: #000080;"><strong>Calling a method</strong></span></h3>
<p>Now we have defined a method, we need to use it. For that, we have to call the method to use its functionality.</p>
<h4><strong><span style="color: #0000ff;">Syntax:</span></strong></h4>
<p>Syntax of calling a method:</p>
<pre>nameOfMethod(Parameter list);</pre>
<p>When calling a method it consists of the method name and a parameter list (number of parameters, type of the parameters and order of the parameters).</p>
<p>The way of calling the<span style="color: #008000;"><strong> max()</strong></span> method which we have defined above:</p>
<pre>max(5, 10);</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Example:</strong></span></h4>
<pre>public class Number {

 public static void main(String[] args) {
 <strong><span style="color: #008000;">//calling max() method</span></strong>
 int result = max(8, 7);
 System.out.print("Greater no: " + result)
 }
 <strong><span style="color: #008000;">//defining max() method</span></strong>
 public static int max(int x, int y) {

 if (x >; y) 
 return x;
 else 
 return y; 
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Output:</strong></span></h4>
<pre>Greater no: 8</pre>
<h3></h3>
<h3><span style="color: #000080;"><strong>Example: Java Methods</strong></span></h3>
<pre>class Addition {

 int result;
 <strong><span style="color: #008000;">//addTwoNumbers() definition
 //method with two parameters and
 //returning integer value.therefore,returnType is int</span></strong>
 public int addTwoNumbers(int a, int b) {

 <strong><span style="color: #008000;">// adding two values.</span></strong>
 result = a + b;

 <strong><span style="color: #008000;">//calling display() method within the same class</span></strong>
 this.display(); 
 
 return result;
 }
 
<span style="color: #008000;"><strong> //display() definition 
 //method with no parameter and
 //returning nothing.therefore,returnType is void</strong></span>
 public void display() {
 System.out.println("display() in Addition class calling inside addTwoNumbers()");
 }
}

public class ExampleDemo {
 public static void main(String[] args) {
 <strong><span style="color: #008000;"> // creating an instance of Addition class</span></strong>
 Addition addition = new Addition();

 <strong><span style="color: #008000;">// calling addTwoNumbers() method to add two values</span></strong>
<strong><span style="color: #008000;"> // using instance created in above step.</span></strong>
 int result = addition.addTwoNumbers(10, 5);
 System.out.println("Addition of two Numbers :" + result);
 } 
}</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Output:</strong></span></h4>
<pre>display() in Addition class calling inside addTwoNumbers()
Addition of two Numbers :15</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Control flow of above program:</strong></span></h4>
<p><img class="alignnone size-full wp-image-1787" src="https://c1ctech.com/wp-content/uploads/2020/04/method_img.png" alt="method_img" width="865" height="617" /></p>
<p> ;</p>


