java why do methods need to be static code example
Example 1: 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 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();
}
}