• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class Methods

 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys I was reading the following topic:
https://coderanch.com/how-to/java/OverridingVsHiding

And I dont know the difference between Class Methods and instance Methods.

1. Class Methods are only the static methods?


*With classMethod() though. since (ahem) it's a class method, the compiler and JVM don't expect to need an actual instance to invoke the method. And even if you provide one (which we did: the instance referred to by f) the JVM will never look at it. https://coderanch.com/how-to/java/OverridingVsHiding

How the JVM knows classMethod() its a classMethod, and why never look to the instance referred?

Thank You!

Kind Regards,
-M
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class methods are static. Non-static methods are instance methods.

Anything static member belongs to the class (hence, the term class methods).
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because class methods are declared with "static" modifier. also to add , you cannot access non-static members(variables and instance members) from static context(block/method). why ? as you wrote yourself, jvm don't expect you to provide any instance when you run static method, so that means it is possible that when you invoke static method, there is no object.
 
Ranch Hand
Posts: 30
Spring Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Furthermore, what is saying is that you can invoke a static method on an instance, but the instance will never be evaluated by the JVM.

 
Rancher
Posts: 1044
6
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pablo Abbate wrote:you can invoke a static method on an instance, but the instance will never be evaluated by the JVM.



Therefore you can invoke a static method even against a null reference without getting a NPE.

 
reply
    Bookmark Topic Watch Topic
  • New Topic