File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Methods" Watch "Class Methods" New topic
Author

Class Methods

Mauro Trevigno
Ranch Hand

Joined: Aug 27, 2012
Posts: 64

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

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Aug 06, 2012
Posts: 30

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.


Nissi Group, Posicionamiento Web, Desarrollo de Software, DiseƱo Web
Blog , LinkedIn
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 908
    
    5
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.

 
jQuery in Action, 2nd edition
 
subject: Class Methods