From: John B. Matthews on
In article
<cba79ebc-a9f8-47df-9b18-bd72b0e84216(a)g19g2000yqe.googlegroups.com>,
Rhino <rchost(a)ymail.com> wrote:

> /**
> * This is a static factory method.
> *
> */
> public StringUtils getInstance() {
> return new StringUtils();
> }
> [...]
> Is this what you have in mind?
>
> If yes, how do I invoke StringUtils.getInstance()? I've tried this
> (from another class) in order to get an instance that uses the default
> Locale:
>
> StringUtils stringUtils = StringUtils.getInstance();
>
> but it generates a compile error because it doesn't like the static
> reference to getInstance(). And this gives the exact same error:
>
> StringUtils stringUtils = new StringUtils.getInstance();
>
> Can you (or anyone else reading this thread) kindly clarify what I've
> got wrong and how to correct it?

public static StringUtils getInstance() {
return new StringUtils();
}

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: markspace on
Rhino wrote:

> In fact, I've created two of them,
> one that takes a Locale as a parameter and one that uses the desfault
> Locale.


Good! That's fine, and good design.


> private static ResourceBundle locMsg = null;
^^^^^^
> private static MessageFormat msgFmt = new MessageFormat(""); //
^^^^^^
> private static Locale locale = null;
^^^^^^
> //Other methods omitted (all of them are static).
^^^^^^
> Is this what you have in mind?


Not exactly. All those static field should be instances variables, not
static. All of the methods except the factory should be non-static
(instance) methods.


>
> If yes, how do I invoke StringUtils.getInstance()? I've tried this

> StringUtils stringUtils = new StringUtils.getInstance();
^^^

As John pointed out you don't call new on a method. Just invoke the
method, remove the new keyword here.
From: Rhino on
On Mar 17, 11:14 am, markspace <nos...(a)nowhere.com> wrote:
> Rhino wrote:
> > In fact, I've created two of them,
> > one that takes a Locale as a parameter and one that uses the desfault
> > Locale.
>
> Good!  That's fine, and gooddesign.
>
> >     private static ResourceBundle locMsg = null;
>
>                ^^^^^^>     private static MessageFormat msgFmt = new MessageFormat(""); //
>
>                ^^^^^^>     private static Locale locale = null;
>
>                ^^^^^^> //Other methods omitted (all of them are static).
>
>                                             ^^^^^^
>
> > Is this what you have in mind?
>
> Not exactly.  All those static field should be instances variables, not
> static.  All of the methods except the factory should be non-static
> (instance) methods.
>
>
>
> > If yes, how do I invoke StringUtils.getInstance()? I've tried this
> >         StringUtils stringUtils = new StringUtils.getInstance();
>
>                                      ^^^
>
> As John pointed out you don't call new on a method.  Just invoke the
> method, remove the new keyword here.

Agreed. Putting a 'new' in front of the StringUtils.getInstance()
didn't make sense to me either. I just wanted you to know that I'd
tried to "think outside the box" in case the coding was a bit
unusual ;-)

Now that I've put the "static" in the definition of getInstance(), the
invocation of getInstance() compiles fine. So I was right that I still
need the two constructors (both private) and that there need to be two
getInstances, one with no parameters and one that expects the Locale
as a parameter?

I'll start removing the 'static' from the variables to turn them back
into instance variables....

Thanks everyone for your help so far. I think I can see the light at
the end of the tunnel now.....

--
Rhino
From: Arne Vajhøj on
On 16-03-2010 23:13, markspace wrote:
> Arne Vajh�j wrote:
>> The reason that they don't is not because it does not make sense
>> from an OO perspective - it is because it would really clutter
>> mathematical code.
>
> Since Rhino (the OP) seems somewhat new to Java, I want to add that
> Arne's comment is speculation. I doubt anyone here knows the actual
> reason the designers of the Java API chose the static pattern they did.

It is speculation that it was what the were thinking.

But it is fact that they have two implementation classes.

It is commonly accepted that with two implementations
an interface is good.

And it seems very likely that they would have been swamped
with complaints if they had made the Math function non-static.

So I would say that the designers should have thought about this
aspect.

They could have tossed something at the recycle bin and
said hit=instance miss=static.

Arne