java static method example
Example 1: why we use static in java
In Java, static keyword is mainly used for memory management.
It can be used with variables, methods, blocks and nested classes.
It is a keyword which is used to share the same variable or method of a given
class. Basically, static is used for a constant variable or a method
that is same for every instance of a class
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 data and static methods in java
class JavaExample{
private static String str = "BeginnersBook";
static class MyNestedClass{
public void disp() {
System.out.println(str);
}
}
public static void main(String args[])
{
JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass();
obj.disp();
}
}