shiva prasad. wrote:
francis varkey wrote:Hi,
What is the consequence if all methods are static ?
thanks
If all the methods are declared as static then you are not required to create any object to that class ,
and you can directly access them using class name.
... and that is one way you would help JVM manage memory from your side. If the abstraction is done only for services and not for data then such a class may not have any attributes in which case if you keep non-static methods in the class you would have to create an instance to call them, an empty object occupies atleast 4 bytes on heap because it has that "super" reference. Now why create an object if you don't need to store any data regarding that object? hence make all methods static so you can call them using class name.
Eg. Calculator class may not have any attributes if all we are concerned about are its methods; make all the methods static and you would call them as
Calculator.add()
Calculator.sub() ...
Library of
Java has many such classes... good example is java.lang.System class where all the methods are static.