Does static methods and class are good for scalability ? I think so static class/method improves scalability of application and instance methods doesn't scales much. So is it good programming practice to write static method where ever it is possible ?
There are three problems to consider with static methods:
- You may introduce a bottleneck if your static method has a large critical region. The largest of course is to declare the whole method synchronized. If it can only be executing one at a time then it's a potential issue;
- Is whatever it's doing still consistent if you're running the same method in different VMs and on different machines? and
- Any method that relies on static methods has problems with unit testing.
It's not generally considered best practice but static helper methods are common. Too complex and another approach should probably be considered.
Does static methods and class are good for scalability?
One has little to do with the other.
I think so static class/method improves scalability of application and instance methods doesn't scales much.
Wrong. Why do you think so?
So is it good programming practice to write static method where ever it is possible?
No. In fact, it is very bad practice because it abandons the maintainability advantages of object-oriented programming.
No. I think you may be assuming that each instance has its own copy of the method definition, taking up that amount of space for each instance, which is not the case.
Editing to add:
In case you wonder how an instance method can be actually shared between the instances: It is because each call to an instance method implicity passes a reference to the instance object to the method. This is generally referred to as the "implicit this" In other words - when you define or call an instance method with two parameters like myMethod(a, b), you can think of it as actually being myMethod(this, a, b), and Java takes care of the this parameter for you, without your having to explicitly define or pass it.
(This, by the way, is handled differently in Python, where you have to explicitly put the object reference in as the first parameter of an instance method definition, though not in the call.)
For an explanation of what goes on at the Java bytecode level here's a link: http://www.artima.com/underthehood/invocationP.html (See stuff around: "The objectref is the implicit this pointer that is passed to any instance method.")
Which form of scalability do you mean? The scalability, that the code is maintainable and extensible in big and small projects? Then using only static-methods hurt. Do you mean performance? If instance-methods are slower (what I don't believe in this generality), then this doesn't mean they don't scale. If they need twice the time as static-methods, they need also twice much time if you call them all 10000 times. Choosing the right algorithms and data-representation decide much more about the scalability of the performance.
At last, if you think static-methods are the way to go, you should not use an object-oriented language like Java. Try C or Pascal or classic Basic-dialects.
No static methods don't intrinsically scale better. Infact the programming style (imperative or object oriented) does not really make any difference to scaling whatsoever. There are two major aspects of scaling and what to do to improve scale depends on which we mean:
1 Scaling by number of requests a second handled
This type of scaling is normally about adding more computers to a cluster to improve overall throughput of the system. Increasing scaling is often about initially reducing the amount of shared resources used through the use of caches and then later making the data access split into shards.
2 Data Scaling
This is when the system gets more and more data over time and operations that access the data (search, filtering etc) get slower as the algorithms are more complex than O(1). In this case the normal strategy is to increase the number of read and write points and use parallel algorithms such as Map/Reduce.
But neither of these aspects has anything to do with whether you use static methods or not, just whether multiple requests work on large sets of data or single sources of data.
It depends on WHY the method is static. If it's static because it truly does not need context, then it will probably scale very well compared to something of similar complexity that is not static because it requires context.
However, if it is static merely because you cannot retain the needed context and must still pass it in, or because of some artificial goal of having more static methods, then I would suspect that it will actually scale LESS than the comparable method as non-static.
In fact I think that ASP Classic proved this point.
Voted down. The OP clearly doesn't quite understand OO. An instance method doesn't take up any extra space when an instance object is created. Static methods aren't going to save you anything unless you're also avoiding creating any instances, in which case you're going so far afield from what an OO language was built for that it's sort of pointless discussion.
I think you're barking up the wrong tree:
In the real world, scalability (or the lack thereof) typically springs from appropriateness of one's algorithms and the efficiency of the operations made against data stores (think: good SQL queries).
Things like whether a method is static or not (or the % of methods which are static) generally have nothing to do with the scalability of a system. You can certainly have a super scalable system which uses no static methods, or super scalable system which uses them exclusively.