1public class MyClass {
2
3 void myMethod() {
4 System.out.println("You have called me! My name is: myMethod!");
5 }
6
7 public static void main(String[] args) {
8 new MyClass().myMethod();
9 }
10}
11
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 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.
1public class Main {
2 public void fullThrottle() {
3 System.out.println("The car is going as fast as it can!");
4 }
5
6 public void speed(int maxSpeed) {
7 System.out.println("Max speed is: " + maxSpeed);
8 }
9}
10