hi if you make a method static it can be accessed without creating the object of the class ..the best example for static method is public static void main(String args) which is the first function to be called before any object is created for that particular class.i.e static method has a scope of class rather than object scope.any further clarifications please write back regards namita ------------------
Joined: Jun 05, 2001
Hi Namita, This is another question asked during the interview. If I answer like the way you said,it is natural to ask, "What if I have to create an object?".So what is your answer now??
Hi Marilyn, The obvious advantage is that there will only be one method in memory regardless of how many objects of the class exist in memory. I am not really sure that there is any performance hit because the JVM still needs to perform the same stack functions (pop, push, adjust, etc) for a class method versus an instance method. Regards, Manfred.
Originally posted by Manfred Leonhardt: The obvious advantage is that there will only be one method in memory regardless of how many objects of the class exist in memory.
hmmmm. . . Methods are loaded into the Method area when a class is loaded. They are not replicated for each instance created. So in either case there is only one method in memory. Only state is maintained in an instance of an object - which means that only the values of the instance variables are kept in the object. The only advantages of static methods are - you don't need to create an object to use them and they don't participate in polymorphism (if this is perceived to be a benefit in some case).
"JavaRanch, where the deer and the Certified play" - David O'Meara
Campbell Ritchie wrote:I don't believe there is any "advantage" to making methods static.
Methods which affect or use instance fields or state must not be static; methods which do not affect or use instance information can beneficially be declared static.
I would say that the advantage to make class Statics is that with a good design, it will make you software more cohesive.
An abstract class, its usually, an class just for concepts. Like a class that makes math calcs. You coud creat an abastract class MathCalcs with the methods plus(); times(); ... For OO concepts, there is no reason for new MathClass().
That is because in the java.lang.Math class, invoking sin or cos does not affect any instance variables. So these methods can beneficially be declared static. Same as I said yesterday. And all the methods in Math don't involve any instance fields, so they can all be static.
Joined: Dec 28, 2006
Thanks for your reactions. The problem is that when your working with interfaces you should use a method on a concrete class. You could encapsulate it however with a static method call.
Methods are loaded into the Method area when a class is loaded. They are not replicated for each instance created
Was interesting for me.
Joined: Oct 13, 2005
By the way: java.lang.Math isn't an abstract class. It is uninstantiable, but that is quite different from abstract.
Campbell Ritchie wrote:By the way: java.lang.Math isn't an abstract class. It is uninstantiable, but that is quite different from abstract.
Yea. But I was not talking about Math java class. I was talking about you make your class math in case if you need some advantage calcs. Like money from US to French or if you want apply some tax on it... And there it goes. With an abstract method you will make sure that all classes that extends you class, will implement it, so the concept will not be lost.
Joined: Oct 13, 2005
I hope you are not going to write a Math class; you get no end of confusion of you write classes with the same name as java.lang classes.
Campbell Ritchie wrote:I hope you are not going to write a Math class; you get no end of confusion of you write classes with the same name as java.lang classes.
Not with the same name. But you are not seeing in the user case that I said. How would you using java.lang.Math, plus 10 (US money) and 10 (Russian Money) and get as result French money? You would have to do your own MatchCalcs class (could be abstract). If you have to extend it and want any sub-class of it to implement a method, you may use abstract methods.