Static methods are as close as possible to a global method. so why is this type of method call not possible? is there any other way to call the static method without instantiating the class??
import java.io.*;
import java.util.*;
class pgm
{
int x,v;
static void func()
{
System.out.println("Function run");
}
}
class egs
{
public static void main(String args[])
{
pgm p=null;
pgm.func();
try
{
p.x=10;
p.func();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
funcisn't a static method. You're trying to call a non-static method without an instance to call it on.egs.func()doesn't exist... I'm not sure why you expect that to work. Static methods are not global, they are still bound to the class. In any case, you don't even have a static method (other than main)ClassName.method()p.x = 10;. p was assigned null and not subsequently reassigned. Therefore, you get a NullPointerException.