Because $item is of type java.lang.Object, $item.getClass() yields a
java.lang.Class representing the class of $item, which has a method
getName() returning a java.lang.String containing its fully qualified
class name. Using Velocity's shortcut syntax which allows us to write
$ref.getSomething() as $ref.something, then the expression
will give you the fully-qualified class name of $item.
If, as in your example, you don't need the classname as a string, then
will tell you whether A is an instance of typeA. Or, if typeA is a
java.lang.Class of the type that you want, it would be
For other possibilities, you have all the public methods of
> -----Original Message-----
> From: rjain [mailto:rjain15@yaho...]
> Sent: Friday, November 10, 2006 9:26 AM
> To: velocity-user@jaka... > Subject: How to get Type of variable
> I have a ArrayList which can contain either items of TypeA or TypeB
> #foreach($item in $arrayList)
> #if ($item <isoftype> TypeA)
> Do something
> #else ($item <isoftype> TypeB)
> Do something else
> How do I get the Type of Item in the Arraylist?
> View this message in context:
> http://www.nabble.com/How-to-get-Type-of-variable-tf2608406.ht ml#a7279003
Sent from the Velocity - User mailing list archive at Nabble.com.