• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Please help on Static Method

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic