File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Class Methods

Mauro Trevigno
Ranch Hand

Joined: Aug 27, 2012
Posts: 96

Hi guys I was reading the following topic:

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.

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

Thank You!

Kind Regards,
James Boswell

Joined: Nov 09, 2011
Posts: 1051

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

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

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

I agree. Here's the link:
subject: Class Methods
It's not a secret anymore!