File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please help on Static Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please help on Static Method" Watch "Please help on Static Method" New topic
Author

Please help on Static Method

Kumanan Guna
Greenhorn

Joined: May 07, 2013
Posts: 9
Hi,

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.

Please help me on this.

adil zahir
Greenhorn

Joined: May 09, 2013
Posts: 29

first you need to understand what that mean


we can write that in this way

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() "
Kumanan Guna
Greenhorn

Joined: May 07, 2013
Posts: 9
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help on Static Method