• Post Reply Bookmark Topic Watch Topic
  • New Topic

Disadvantage of Static Method  RSS feed

 
Varadhan Sesharaman
Ranch Hand
Posts: 30
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys ,

I want to know what is the main disadvantage of using many no of static methods in a class.

Can you guys please list out the disadvantages and the reason behind it.

Thanks
 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with static methods you cannot use polymorphism -> you loose one of the biggest advantege of OOP
you can access only static variables, so you cannot run methods on different objects with different states
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varadhan Sesharaman wrote: main disadvantage of using many no of static methods in a class.

do you concern about performance? If yes, Please dont worry about it . for instance,see java.util.Arrays
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel that using a lot of static methods in a class would make the program not so Object oriented, you drive the program to be more like consisting of procedures which are invoked by different parts of the code. So picking whether its static or not has to be done carefully considering the current design and possible changes to the code.

There are cases where all the methods are static in a class- for example consider the java.uti.Arrays as pointed out by Seetharaman. In this case Arrays class is not bound to any instance or it doesnt hold any state, its just a Array utility class.

 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you should look at the purpose of the particular code you're writing.

The main reason you should declare a method static is when you are writing code that has no relation to an instance of any class. F.e. performing a certain calculation that's the same everytime, or accessing a file on disk.

In my opinion, it would be silly to force someone to instantiate a class (which allocates memory), in order to access a method that has nothing to do with that instance.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!