accessing a static method in java code example
Example 1: how to call a static method in java
class scratch{
public static hey(){
System.out.println("Hey");
}
public static void main(String[] args){
hey();
//print Hey to the console
}
Example 2: static data and static methods in java
class JavaExample{
private static String str = "BeginnersBook";
//Static class
static class MyNestedClass{
//non-static method
public void disp() {
/* If you make the str variable of outer class
* non-static then you will get compilation error
* because: a nested static class cannot access non-
* static members of the outer class.
*/
System.out.println(str);
}
}
public static void main(String args[])
{
/* To create instance of nested class we didn't need the outer
* class instance but for a regular nested class you would need
* to create an instance of outer class first
*/
JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass();
obj.disp();
}
}