I'm preparing for OCPJP 6 exam and study guide as K&B book.
One of the question as calling non-static method from main static method. But as per my understanding we cannot call non-static method from static method.
As main method is a static, would like to know how below example works fine.
Method go() is non-static method, but we are calling that method from main static method.
we create Object Zookeeper and we use p reference to point to the Object Zookeeper , now we will invoke the method go() .
yes we can't call no-static method directly from static method but we can use instance Object ( create Object the same way we did in the example "Zookeeper p = new Zookeeper(); " ) to call the no-static method " p.go() "
Joined: May 07, 2013
Thank you Zahir...
Yes, I did trail and error by calling the go() method directly and got an expected error "cannot make a reference to static from non-static method".
so, only through the instance object non-static method can be invoked from static methods.