Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

static methods?

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally it is recommended that static methods only be used as "utility methods" which return some value calculated solely from data passed to the method as parameters. They are used because they dont use or access the state of an object.
In my application I have defined a particular method as static as it takes some input and provides ouput without needing access to any onject state information. However as i was coding the method i discovered that i needed some functionality which could be best provided by a recursive method. However to call this method from my static method the recursive method will also have to be static ( i would more than likely make this private ) .
What i am wondering is if this is a bad design/coding practice. Is it bad to have a number of static methods within a class and for one to call the other. Even though there is no object state involved is there a case for replacing these static utility methods with instance methods??
John
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only real problem with static methods is that they cannot be overridden in a subclass. Having several static methods which call each other is not a problem, especially as some of them can be private if you don't want objects of other classes to be able to call them.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods can be overridden by a subclass, final methods can't. I think Eckel describes the use of static well in Chapter 4, of his book "Thinking in Java", http://www.mindview.net/Books
The meaning of static
With the this keyword in mind, you can more fully understand what it means to make a method static. It means that there is no this for that particular method. You cannot call non-static methods from inside static methods (although the reverse is possible), and you can call a static method for the class itself, without any object. In fact, that�s primarily what a static method is for. It�s as if you�re creating the equivalent of a global function (from C). Except global functions are not permitted in Java, and putting the static method inside a class allows it access to other static methods and to static fields.
Some people argue that static methods are not object-oriented since they do have the semantics of a global function; with a static method you don�t send a message to an object, since there�s no this. This is probably a fair argument, and if you find yourself using a lot of static methods you should probably rethink your strategy. However, statics are pragmatic and there are times when you genuinely need them, so whether or not they are "proper OOP" should be left to the theoreticians. Indeed, even Smalltalk has the equivalent in its "class methods."
[ July 22, 2002: Message edited by: Dan Cranmer ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan - you can create a static method in a subclass which has the same profile as a static method in a superclass. However this isn't overriding - it's hiding. See this example:

It may look like classMethod() in Sub would override classMethod() in Base, but it's not happening. Dynamic polymorphismis inherent in what we mean when we say a method overrides another, and there is no dynamic polymorphism associated with static methods.
[ July 22, 2002: Message edited by: Jim Yingst ]
added a missing /URL tag on a link
[ July 23, 2002: Message edited by: Frank Carver ]
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies guys ( especially Frank who has answered a number of my questions lately ) . I am going to leave the methods static for now. I dont think i will need to subclass as the class does not have any functionality other than to hold 2/3 utility methods.
As my application evolves perhaps my requirements of the class will change and i can think about implementing them as instance methods........
 
Dan Cranmer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim! I didn't know there was a distinction between overriding and hiding, thanks for the clarification.
 
No, tomorrow we rule the world! With this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic