static in main method in java code example

Example 1: why main method static

Because the object is not required to call the static method. 
If we make the main method non-static, 
JVM will have to create its object first and then call main() method which 
will lead to the extra memory allocation.

Example 2: do i have to use static methods in java main

Use a static method 
when you want to be able to access the method 
without an instance of the class

Example 3: Static method in java

We can declare a method as static by adding keyword “static” before method name.
Let’s see example on static method in java.

public class StaticMethodExample
{
   static void print()
   {
      System.out.println("in static method.");
   }
   public static void main(String[] args)
   {
      StaticMethodExample.print();
   }
}

Tags:

Misc Example