You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by John <jc...@gmail.com> on 2006/12/19 00:04:50 UTC

Question about Utility class in the VelocityContext

Hi all,

I'm getting familiarized with Velocity, and I'm wondering about how I should
handle the following case:

I have a utility class called "Utils". Now typically if I wanted to use the
member functions in this class in my other Java classes I would declare them
static and reference them as so "Utils.checkInput(int)".

Now with Velocity and the idea that if I want these utilities available to
the VTL, I understand I need to put the object in the VelocityContext. From
what I have seen (for example with the VelocityFormatter tool), a new object
is created and put in the context

velocityContext.put("formatter", new VelocityFormatter());

So my question is: what do I do with my Utils class? Should I create a new
instance as well? Does that mean I should change my static methods to
non-static methods? I guess my inexperience is showing here :)

Thanks!
John

Re: Question about Utility class in the VelocityContext

Posted by Claude Brisson <cl...@renegat.net>.
This is a question for the user list.

Le lundi 18 décembre 2006 à 16:04 -0700, John a écrit :
> Hi all,
> 
> I'm getting familiarized with Velocity, and I'm wondering about how I should
> handle the following case:
> 
> I have a utility class called "Utils". Now typically if I wanted to use the
> member functions in this class in my other Java classes I would declare them
> static and reference them as so "Utils.checkInput(int)".
> 
> Now with Velocity and the idea that if I want these utilities available to
> the VTL, I understand I need to put the object in the VelocityContext. From
> what I have seen (for example with the VelocityFormatter tool), a new object
> is created and put in the context
> 
> velocityContext.put("formatter", new VelocityFormatter());
> 
> So my question is: what do I do with my Utils class? Should I create a new
> instance as well? Does that mean I should change my static methods to
> non-static methods? I guess my inexperience is showing here :)

Why can't you create a new instance of Utils? Static methods can be
called as if they were instance methods.

  Claude

> Thanks!
> John


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Question about Utility class in the VelocityContext

Posted by Henning Schmiedehausen <hp...@intermeta.de>.
Well, you wouldn't have. As the methods are static they are not part of 
an instance, so the actual object would probably have only 16 bytes or 
so. But your solution is obviously as good or even superior.

	Best regards
		Henning



John schrieb:
> I wanted to avoid having multiple copies of the Utils class lying around 
> in memory, so I switched to using a singleton Utils class, that I can 
> instantiate and add to the Velocity context.
> 
> Thanks guys!
> John
> 
> On 12/21/06, *Henning P. Schmiedehausen* <hps@intermeta.de 
> <ma...@intermeta.de>> wrote:
> 
>     John <jcubed@gmail.com <ma...@gmail.com>> writes:
> 
>     Hi,
> 
>     IIRC, you can add a public C'tor to construct a dummy object and then
>     use that in the context. I remember that something like this is the
>     reason why some of the Utility classes in Jakarta commons have a
>     public, no-arguments constructor even though the classes contain only
>     static methods.
> 
>     A while ago, we talked about "invocation of static methods" but I'm
>     not sure if that discussion ever went anyplace.
> 
>             Best regards
>                     Henning
> 
> 
> 
>      >------=_Part_26899_4319714.1166483090496
>      >Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>      >Content-Transfer-Encoding: 7bit
>      >Content-Disposition: inline
> 
>      >Hi all,
> 
>      >I'm getting familiarized with Velocity, and I'm wondering about
>     how I should
>      >handle the following case:
> 
>      >I have a utility class called "Utils". Now typically if I wanted
>     to use the
>      >member functions in this class in my other Java classes I would
>     declare them
>      >static and reference them as so " Utils.checkInput(int)".
> 
>      >Now with Velocity and the idea that if I want these utilities
>     available to
>      >the VTL, I understand I need to put the object in the
>     VelocityContext. From
>      >what I have seen (for example with the VelocityFormatter tool), a
>     new object
>      >is created and put in the context
> 
>      >velocityContext.put("formatter", new VelocityFormatter());
> 
>      >So my question is: what do I do with my Utils class? Should I
>     create a new
>      >instance as well? Does that mean I should change my static methods to
>      >non-static methods? I guess my inexperience is showing here :)
> 
>      >Thanks!
>      >John
> 
>      >------=_Part_26899_4319714.1166483090496--
> 
>     --
>     Henning P. Schmiedehausen  -- hps@intermeta.de
>     <ma...@intermeta.de> | J2EE, Linux,
>     91054 Buckenhof, Germany   -- +49 9131 506540 | Apache person
>     Open Source Consulting, Development, Design | Velocity - Turbine guy
> 
>               "Save the cheerleader. Save the world."
> 
>     ---------------------------------------------------------------------
>     To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
>     <ma...@velocity.apache.org>
>     For additional commands, e-mail: dev-help@velocity.apache.org
>     <ma...@velocity.apache.org>
> 
> 

-- 
Henning P. Schmiedehausen  -- hps@intermeta.de | J2EE, Linux
91054 Buckenhof, Germany   -- +49 9131 506540  | Apache person
Open Source Consulting, Development, Design    | Velocity - Turbine

	  "Save the cheerleader. Save the world."

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Question about Utility class in the VelocityContext

Posted by John <jc...@gmail.com>.
I wanted to avoid having multiple copies of the Utils class lying around in
memory, so I switched to using a singleton Utils class, that I can
instantiate and add to the Velocity context.

Thanks guys!
John

On 12/21/06, Henning P. Schmiedehausen <hp...@intermeta.de> wrote:
>
> John <jc...@gmail.com> writes:
>
> Hi,
>
> IIRC, you can add a public C'tor to construct a dummy object and then
> use that in the context. I remember that something like this is the
> reason why some of the Utility classes in Jakarta commons have a
> public, no-arguments constructor even though the classes contain only
> static methods.
>
> A while ago, we talked about "invocation of static methods" but I'm
> not sure if that discussion ever went anyplace.
>
>         Best regards
>                 Henning
>
>
>
> >------=_Part_26899_4319714.1166483090496
> >Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> >Content-Transfer-Encoding: 7bit
> >Content-Disposition: inline
>
> >Hi all,
>
> >I'm getting familiarized with Velocity, and I'm wondering about how I
> should
> >handle the following case:
>
> >I have a utility class called "Utils". Now typically if I wanted to use
> the
> >member functions in this class in my other Java classes I would declare
> them
> >static and reference them as so "Utils.checkInput(int)".
>
> >Now with Velocity and the idea that if I want these utilities available
> to
> >the VTL, I understand I need to put the object in the VelocityContext.
> From
> >what I have seen (for example with the VelocityFormatter tool), a new
> object
> >is created and put in the context
>
> >velocityContext.put("formatter", new VelocityFormatter());
>
> >So my question is: what do I do with my Utils class? Should I create a
> new
> >instance as well? Does that mean I should change my static methods to
> >non-static methods? I guess my inexperience is showing here :)
>
> >Thanks!
> >John
>
> >------=_Part_26899_4319714.1166483090496--
>
> --
> Henning P. Schmiedehausen  -- hps@intermeta.de | J2EE, Linux,
> 91054 Buckenhof, Germany   -- +49 9131 506540 | Apache person
> Open Source Consulting, Development, Design | Velocity - Turbine guy
>
>           "Save the cheerleader. Save the world."
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
>

Re: Question about Utility class in the VelocityContext

Posted by "Henning P. Schmiedehausen" <hp...@intermeta.de>.
John <jc...@gmail.com> writes:

Hi, 

IIRC, you can add a public C'tor to construct a dummy object and then
use that in the context. I remember that something like this is the
reason why some of the Utility classes in Jakarta commons have a
public, no-arguments constructor even though the classes contain only
static methods.

A while ago, we talked about "invocation of static methods" but I'm
not sure if that discussion ever went anyplace.

	Best regards
		Henning



>------=_Part_26899_4319714.1166483090496
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>Content-Transfer-Encoding: 7bit
>Content-Disposition: inline

>Hi all,

>I'm getting familiarized with Velocity, and I'm wondering about how I should
>handle the following case:

>I have a utility class called "Utils". Now typically if I wanted to use the
>member functions in this class in my other Java classes I would declare them
>static and reference them as so "Utils.checkInput(int)".

>Now with Velocity and the idea that if I want these utilities available to
>the VTL, I understand I need to put the object in the VelocityContext. From
>what I have seen (for example with the VelocityFormatter tool), a new object
>is created and put in the context

>velocityContext.put("formatter", new VelocityFormatter());

>So my question is: what do I do with my Utils class? Should I create a new
>instance as well? Does that mean I should change my static methods to
>non-static methods? I guess my inexperience is showing here :)

>Thanks!
>John

>------=_Part_26899_4319714.1166483090496--

-- 
Henning P. Schmiedehausen  -- hps@intermeta.de | J2EE, Linux,
91054 Buckenhof, Germany   -- +49 9131 506540 | Apache person
Open Source Consulting, Development, Design | Velocity - Turbine guy

          "Save the cheerleader. Save the world."

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org