• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with Static Mehtods  RSS feed

 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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.

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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?
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!