• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Methods

 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I was reading the following topic:
https://www.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://www.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
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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).
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Pablo Abbate
Ranch Hand
Posts: 30
Java Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic