Skip to main content

Static methods

Static Methods | Java: Static methods are having collection of statements to perform particular task.

Syntax:                              static void/int method_name(par1,par2,...,parn)
                                          {
                                                         // statements.
                                           }

Here is the Code:

  1. import java.io.*;
  2. class Main {
  3. public static void main (String[] args) {
  4. A();
  5. B();
  6. print("Hello");
  7. sum(1,2);
  8. int a = mult(1, 2);
  9. System.out.println(a);
  10. }
  11. static void A()
  12. {
  13.     System.out.println("I am in A");
  14. }
  15. static void B()
  16. {
  17.     System.out.println("I am in B");
  18. }
  19. static void print(String str)
  20. {
  21.     System.out.println(str);
  22. }
  23. static void sum(int a,int b)
  24. {
  25.     System.out.println(a+b);
  26. }
  27. static int mult(int a,int b)
  28. {
  29.     return a * b;
  30. }
  31. }

Output:
I am in A
I am in B
Hello
3
2

Here is the Video: