why the main method is static 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: 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();
}
}