• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

static methods

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the advantage of making a method static?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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??
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you HAVE to create an object then you don't have to declare the method static.
 
Marilyn Monickam
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for making sure. The conclusion of 2001 is still valid in 2010.

 
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic