Home >Java >javaTutorial >Methods in Java
A java method can be defined as a set of logical java statements written to perform a specific task. They provide a way to reuse code without writing the code again. In Java, any method should be part of a different class from Python, C, and C++. The existence of methods is not possible without a java class. Here is the list of components involved while creating java methods:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Here is the list of components involved while creating java methods:
Syntax:
Here is a basic syntax of methods:
//declare Enclosing class public class Myclass{ //declare java method public String concat(String s1, String s2){ // combine two strings with space String s3= s1 + " " + s2 ; //return resulting string return s3; } }
Methods can be categorized into the following two types:
Moreover, you can also get and set methods in Java by addressing experts from AssignmentCore who will provide you with Java homework help of any complexity.
When a calling program calls a method, the control goes into the method body. After control goes to the method body, it returns to the calling program under the following three conditions:
Static methods are called using the class name, and non-static methods are called using object instance.
Now we will see java code examples show how methods are declared and called using java. In this example, we will see how to create a static method and how is it called.
Code:
package com.edubca.methods; public class MethodDemo{ public static int getMaximum(int a , int b){ if(a>b){ return a; }else { return b; } } public static void main (String args[]){ int maxvalue1 = getMaximum(10,23); System.out.println("Out of 10 and 23, " + maxvalue1 + " is greater" ); int maxvalue2= getMaximum(40,20); System.out.println("Out of 40 and 20, " + maxvalue2 + " is greater" ); } }
Output:
In the next example, we will see how to call non–static methods.
Code:
package com.edubca.methods; public class MethodDemo{ public int getMinimum(int a , int b){ if(a<b){ return a; }else { return b; } } public static void main (String args[]){ MethodDemo demo =new MethodDemo(); int minvalue1 = demo.getMinimum(10,23); System.out.println("Out of 10 and 23, " + minvalue1 + " is smaller" ); int minvalue2= demo.getMinimum(40,20); System.out.println("Out of 40 and 20, " + minvalue2 + " is smaller" ); } }
As we can see above, an instance of an enclosing class is required to call a non-static method. The above code will produce the following output:
Output:
In the next example, we how to create methods throwing exceptions.
Code:
import java.io.*; package com.edubca.methods; public class MethodDemo{ public void mymethod() throws IOException{ throw new IOException("IO Exception occurred..."); } public static void main (String args[]){ MethodDemo demo =new MethodDemo(); try{ demo.mymethod(); }catch(Exception e){ e.printStackTrace(); } } }
As we can see from the above code, whenever a method throws an exception caller, the method must handle the exception using try-catch or any other suitable error handling mechanism. The above code shows the below output on screen:
Output:
From the above article, we have a clear idea about methods in java. Therefore will the help of methods, we can achieve any task. Using methods make our code reusable and easy to test, understand and debug.
The above is the detailed content of Methods in Java. For more information, please follow other related articles on the PHP Chinese website!