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:
- import java.io.*;
- class Main {
- public static void main (String[] args) {
- A();
- B();
- print("Hello");
- sum(1,2);
- int a = mult(1, 2);
- System.out.println(a);
- }
- static void A()
- {
- System.out.println("I am in A");
- }
- static void B()
- {
- System.out.println("I am in B");
- }
- static void print(String str)
- {
- System.out.println(str);
- }
- static void sum(int a,int b)
- {
- System.out.println(a+b);
- }
- static int mult(int a,int b)
- {
- return a * b;
- }
- }
Output:
I am in A
I am in B
Hello
3
2
Here is the Video: