• 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

Problems with Static Mehtods

 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one type the difference between the normal object methods and static methods? mostly in problem oriented direction.

Thank you.

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivas Kalvala:
Can any one type the difference between the normal object methods and static methods? mostly in problem oriented direction.

Thank you.



I don't know exactly what you really want to know..

But ok let me try..

Difference Between Static Method & Normal Methods

>< Instance methods are associated with an object and use the instance variables of that object. This is the default.

>< Static methods use no instance variables of any object of the class they are defined in. If you define a method to be static, you will be given a rude message by the compiler if you try to access any instance variables. You can access static variables, but except for constants, this is unusual. Static methods typically take all they data from parameters and compute something from those parameters, with no reference to variables. This is typical of methods which do some kind of generic calculation. A good example of this are the many utility methods in the predefined Math class.

With Normal Method if you need to invoke them then you have to create an Object of Class and then need to call the method with that Object.

But in Case of Static Methods you may directly call method with refernce of Class Directly.

This is the biggest use of Static methods.

There are so many built-in API's in so many classes which are declared as static so that we may directly call without instantiating the Class.

Hope this helps you..

If still any concern then revert me with exact concerns...
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for you prompt response !

But I want to know the problems with static methods. The situations where we can avoid the static methods.

Thank you.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no problem with static methods as such. Sometimes it's necessary to call methods of a class if no instance of that class is available, or if it doesn't make sense to specify a particular instance. That's when you'd use them.

If you wish to avoid having static methods for some reason, simply don't declare any (by leaving out the "static" in their declaration). Then they become instance methods.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be fishing for something like this ... When you invoke a static method with a classname

you have a hard-coded reference and dependency on MyClass. It is not possible to us polymorphism and work with an instance of some other class. For example I use a vendor provided logger ...

We found some problems with LogHelper, but it's impossible to subclass the logger to do different things or to make other existing code use a new logger. Other loggers do something more like this:

Now we can modify LogHelper to return a new implementation of the Logger interface. We can have several versions that we plug in via configuration.

Does that illustrate one issue with static?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One serious problem with static methods is that using them significantly reduces testability, since they can't be mocked in any simple way. As Ulf noted, there are situations when they are useful, but IMO they should be avoided as far as possible.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic