Method is a block of code which only runs when it is called, methods will be created inside a java class. Methods are used to perform certain actions, and they are also known as functions. The main use of methods in java is we can club a specific reusable code in a method and which can be reused whenever it needed.
Every method must be declared within the class, java doesn't allow to declare a method outside the class. We need to follow below syntax to delcare a methods in java.
Syntax: to create our own user defined methods in java
returnType methodName() {
// logic
}
Below example will show how to use a method in java class: Example-1:
public class DemoExample1 {
public static void main(String args[]) {
Demo d = new Demo(); // object creation
d.additionOperation(); // calling or invoking the user-defined method
}
void additionOperation() { // this is user defined method
int a = 10;
int b = 20;
int c = a+b;
System.out.println("addition of two numbers = "+c);
}
}
Example Explained
In above example first we have created the java class and inside this calss we have declared a main method. Outside a main method we have declared another method (user defined method) to perform or implement an addition operation. This user defined method can be call or invoke from the main method by creating a object of a class.
Object of a class can be created using new Keyword.
Syntax: to create a object in java is:
className reference = new className();
Below example will show how to invoke a method which is available in another java class: Example-2:
// Demo1 java class which contains main method
public class DemoExample2 {
public static void main(String args[]) {
Addition a = new Addition(); // create an object of Addition java class
a.add(); // invoking or calling a method to execute a user-defined (add) method.
}
}
// Addition java class which contains user-defined method
public class Addition {
void add() {
int a = 90;
int b = 10;
int c = a+b;
System.out.println("Addition of two numbers = "+c);
}
}
Example 3:
// Demo1 java class which contains main method
public class DemoExample3 {
public static void main(String args[]) {
Addition a = new Addition(); // create an object of Addition java class
int result = a.add(); // invoking or calling a method to execute a user-defined (add) method.
System.out.println("Result is = "+result);
}
}
// Addition java class which contains user-defined method
public class Addition {
int add() {
int a = 90;
int b = 10;
int c = a+b;
return c; // added the return type
}
}
Example 4:
// Demo1 java class which contains main method
public class DemoExample4 {
public static void main(String args[]) {
Addition a = new Addition(); // create an object of Addition java class
int a = 3000;
int b = 2000;
int result = a.add(a, b); // invoking or calling a method to execute a user-defined (add) method.
System.out.println("Result is = "+result);
}
}
// Addition java class which contains user-defined method
public class Addition {
int add(int a, int b) {
int c = a+b;
System.out.println("Addition result = "+c);
return c; // added the return type
}
}