1class scratch{
2 public static hey(){
3 System.out.println("Hey");
4 }
5 public static void main(String[] args){
6 hey();
7 //print Hey to the console
8 }
1Use a static method
2when you want to be able to access the method
3without an instance of the class
1We can declare a method as static by adding keyword “static” before method name.
2Let’s see example on static method in java.
3
4public class StaticMethodExample
5{
6 static void print()
7 {
8 System.out.println("in static method.");
9 }
10 public static void main(String[] args)
11 {
12 StaticMethodExample.print();
13 }
14}