aspose file tools*
The moose likes Beginning Java and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static methods" Watch "static methods" New topic
Author

static methods

Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
What is the advantage of making a method static?
namita pa
Ranch Hand

Joined: Sep 06, 2001
Posts: 56
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
------------------
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66

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??
Margaret Tan
Greenhorn

Joined: Sep 14, 2001
Posts: 20
If you HAVE to create an object then you don't have to declare the method static.
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi
That is why I want to know what the advantage is.Does it reflect on the performance if I create an object and then invoke the method.I don't think so.

Marilyn
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Originally posted by Marilyn Monickam:
That is why I want to know what the advantage is. Does it reflect on the performance if I create an object and then invoke the method.I don't think so.
Marilyn

You are wasting your energy and causing possible confusion if you create an object and then invoke a static method on that object.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Just for making sure. The conclusion of 2001 is still valid in 2010.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

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().

It's just about OO and Good Design.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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.

The comment:

Methods are loaded into the Method area when a class is loaded. They are not replicated for each instance created


Was interesting for me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
By the way: java.lang.Math isn't an abstract class. It is uninstantiable, but that is quite different from abstract.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods