This question already has an answer here:
I'm getting an error when I try to call a non-static method in a static class.
Cannot make a static reference to the non-static method methodName() from the type playback
I can't make the method static as this gives me an error too.
This static method cannot hide the instance method from xInterface
Is there any way to get round calling an non-static method in another static method? (The two methods are in seperate packages and seperate classes).
I use an interface and create an anonymous instance of it like so:
AppEntryPoint.java
Main.java
Not as elegant as creating an instance of that class and calling its own method, but accomplishes the same thing, essentially. Just another way to do it.
the above code not executed because static method must have that class reference.
This will be definitely get executed. Because here we are creating reference which nothing but "sm" by using that reference of that class which is nothing but (
StaticMethod=new Static method()
) we are calling method one (sm.methodOne()
).I hope this will be helpful.