• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Confusion about static vs instance methods in Java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Post Content
Hi Ranchers,
I’m learning Java and I have some confusion about the difference between static methods and instance methods.
I understand that:
Static methods belong to the class
Instance methods belong to objects
But I’m not clear about:
When should I prefer a static method over an instance method?
Why static methods cannot directly access instance variables?
Is it a bad practice to use many static methods in real projects?
A simple explanation with a small example would really help.
Thanks in advance!
 
Bartender
Posts: 11226
91
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods can only access other static methods or static class members.
Instance methods can access both static and instance methods and both instance an static class data members.
Static means that there is only one defined for the class regardless of how many instances exist.
Static is a indicator to you that you are about to write non-object oriented code which may temporarily stop compiler error messages but then lead you down the wrong path when you should really be writing object oriented code.
When refactoring large methods it is  typical to extract small bits of logic into their own helper method. Quite often these methods do not need to operate on instance data which means you  should  make them static.
 
Rancher
Posts: 5304
87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Accessibility is determined by modifier such as public, private, protected.  It has nothing to do with whether a method is a static method or and instance method.

Static methods can access instance methods and instance fields, if they are in the same class, or if they have appropriate access modifier- but they need to specify an instance to do it; there is no "this" reference, implicit or explicit.  So from  a static method, you need something like "x.method()", rather than just "method()".  Where x is a reference that was either passed into the method, or created within the method.
 
Bartender
Posts: 29139
215
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just as a practical observation, more often than not when I've defined a method as a static method I eventually end up having to convert it to an instance method.

There are times when a static method is the way to go, but most often, instance methods are what you'll need.

Here's an example of what I mean.

I define a class for money conversion, Say, US dollars to Euros and back. I could have a class with a static method for each direction.

But currency conversion rates fluctuate. Suppose I have a system where I need to make the conversion in effect at a given time/date? If I made the conversion rate a static property, that would be impossible. But if I made e  converter instances, each with its own rate, I could work with a whole set of times and just swap them in and out.

And what if I want to convert to/from Pounds Sterling s in a way I can plug in this alternate converter in place of the original dollar/Euro converter? You can likewise subclass an abstract currency converter for that so that the currency types are also plug-replaceable.

One of the classic cases where I've had to refactor static methods to be instance methods was a temperature converter. You'd think that that would be too simple to make instances of, but again, there are more than just two temperature scales out there.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Use a static method when the behavior does not depend on object data.It is suitable for common functionality that is the same for everyone and does not require instance variables.
2. Because static methods belong to the class, while instance variables belong to objects. A static method does not have any object reference, so it does not know which object’s data to use.
3. Not always. It is fine for utility or helper methods.It is not good if most of your business logic is static, because that breaks object-oriented design principles like polymorphism and flexibility.

If logic depends on object state : use instance method
If logic is common and independent : use static method


Calling Method :
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic