1public class Main {
2 public static void main(String args[]) {
3 SayHi();
4
5 int sum = AddNums(5, 6);
6 System.out.println(sum);
7 }
8
9 public static void SayHi() { //This method has no return value
10 System.out.println("Hi!");
11 }
12
13 public static int AddNums(int a, int b) { //This method has a return value
14 return a + b;
15}
1public class Main {
2 static void myMethod() {
3 // code to be executed
4 }
5}
6
7
1public class Main
2{
3 static void function(){
4 System.out.println("I am a function!");
5 }
6 public static void main(String[] args) {
7 function();
8 }
9}
1public class MyClass {
2 static int myMethod(int x) {
3 return 5 + x;
4 }
5
6 public static void main(String[] args) {
7 System.out.println(myMethod(3));
8 }
9}
10// Returns 8
1A method in java is a group of statements to carry out some operation also
2known as functions.
1Method is a collection of statements
2which returns a value upon its execution
3Method have a return and the method's name may or not be same as the class
4name.
5Method is invoked explicitly.
6Method is not provided by compiler in any case.
7Methods are inherited by child classes.