The Artima Developer Community
Sponsored Link

Java Answers Forum
static util methods?

6 replies on 1 page. Most recent reply: Mar 25, 2004 11:25 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
N

Posts: 2
Nickname: nawt
Registered: Mar, 2004

static util methods? Posted: Mar 24, 2004 7:09 PM
Reply to this message Reply
Advertisement
<pre>
I have always wondered which approach is better for utility
methods. Here are two approaches.

Part A.
public class UserManager
{
privat String timeLoggedIn(String userId)
{
return convertDate(loginTime);
}

// A.1.
private static String convertDate(Date date)
{
return "time is " + date.toString();
}

// A.2.
private static String convertDate(Date date)
{
return "time is " + date.toString();
}
}

Is first approach going back to procedural programming?
The advantage is, some other class can call convertDate
without instantiating UserManager. Then ofcourse,
convertDate goes into Util class. But still, is it okay to
have static methods so that other classes can share the
method without instantiating?


Part B.
Now if that shared method is moved to utility class:
// B.1.
public class Util
{
public static String formatDate(Date date)
{
return "Date is " + date.toString();
}
}
// B.2.
public class Util
{
public String formatDate(Date date)
{
return "Date is " + date.toString();
}
}

Is it better to use static method:
Util.formatDate(somedate);
or should we access the method as:
(new Util()).formatDate(somedate);

Ofcourse, we can limit number of instances created in
second case by creating a singleton, but thats besides the
point.

Thanks for your opinion/suggestions/ideas.
</pre>


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: static util methods? Posted: Mar 24, 2004 7:16 PM
Reply to this message Reply
IMHO, if the method doesn't need an object, then it might as well be static. If you look at the methods in the java.lang.Math class, they are all static. In fact, I read somewhere that you cannot even instantiate a Math object (though I don't know this for a fact).

If Sun can make a whole class of static methods, why can't you?

Sandu Crasteti

Posts: 13
Nickname: xan
Registered: Feb, 2003

Re: static util methods? Posted: Mar 25, 2004 1:17 AM
Reply to this message Reply
Question for twc:

Why would you instantiate an object of a class (like Math) that has only static (class) methods?

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: static util methods? Posted: Mar 25, 2004 9:22 AM
Reply to this message Reply
> Question for twc:
>
> Why would you instantiate an object of a class (like Math)
> that has only static (class) methods?

You wouldn't. But, if a class has both static and non-static methods, you can call the static methods from an instantiated object. Why you would is another question.

What I read indicated that Sun had made the constructor of the Math class private so you cannot create a Math object even if you (for some unknown reason) wanted to.

N

Posts: 2
Nickname: nawt
Registered: Mar, 2004

Re: static util methods? Posted: Mar 25, 2004 10:19 AM
Reply to this message Reply
Well, my concern is that if I have choice of making a method static vs. non static, which way should I lean keeping the following in mind:
1. how does it affect performance? (in a heavily stressed multithreaded application)
2. design, with OOPs in mind.
3. with too many statics around, will my architecture tend to be procedural?
4. aesthetics.
5. anything else that I dont know?

Thanks.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: static util methods? Posted: Mar 25, 2004 11:12 AM
Reply to this message Reply
> Well, my concern is that if I have choice of making a
> method static vs. non static, which way should I lean
> keeping the following in mind:
> 1. how does it affect performance? (in a heavily stressed
> multithreaded application)
> 2. design, with OOPs in mind.
> 3. with too many statics around, will my architecture tend
> to be procedural?
> 4. aesthetics.
> 5. anything else that I dont know?
>
> Thanks.

The key phrase here is "if I have a choice." You don't have a choice with instance (non-static) methods, they cannot be static. If you have a choice, then that means that the method has nothing to do with an instance of the class and therefore should be static.

One caveat. If you find yourself only making static methods, then you are probably not using OOP design. That, however, is another issue.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: static util methods? Posted: Mar 25, 2004 11:25 AM
Reply to this message Reply
static methods can also be inflexible. In some cases, it makes sense to have a singleton object that provides the methods (with a static getInstance(), of course). This can also facilitate unit testing and make later extension or changes much easier.

Providing this flexibility can be worthwhile, especially when there is a little bit of state associated with the operations involved.

For example, I created a "localized string provider" class (not in Java) that I made into a singleton object. I could have just had a static method getString( id, params ) that would have worked. Later when I wanted to use this facility in a different context, I was thankful that I had done it this way; by being able to substitute a different object (implementing the same interface, of course), it was a snap.

Certainly don't use performance as a justification for, or a reason to use static methods. Firstly, that's an eggregious act of premature optmization. Secondly, if testing and profiling reveals a real problem, making the singleton's method final should yield the same performance as a static method, since they are both not virtual (anyway, the slowness-of-virtual-methods bugaboo is being addressed well by JITs these days; see the last couple installments of Bill's interviews with Anders Hejlsberg on this, eg. http://www.artima.com/intv/choices.html).

I think static methods are good for utility methods which have and will never have any kind of state except that passed in by parameters. Some examples might be miscellaneous utility or helper functions, such as adding capabilities to the built-ins like the String class or the File class.

[/b]static[/b] methods can often be a little too static, so they should be used judiciously.

Flat View: This topic has 6 replies on 1 page
Topic: convert an int into a byte array & vice versa Previous Topic   Next Topic Topic: TSR Programm

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use