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 MyClass {
2
3 static void myMethod() {
4 System.out.println("You have called me! My name is: myMethod!");
5 }
6
7 public static void main(String[] args) {
8 myMethod();
9 }
10}
1//declare a function like this:
2void function(int parameter1)
3{
4 //function body
5}
6//call the function like this:
7function(1);
1A method in java is a group of statements to carry out some operation also
2known as functions.
1public int addNum(int num1, int num2) {
2 total = num1 + num2;
3 System.out.println("Total: " + total);
4}