Prev: Open popup from JSP - Servlet ??
Next: Date comparisons
From: angelochen960 on 12 Mar 2010 09:02 Hi, I have this bean: public class Item { private String type; private Boolean pub; public String getType() { return type;} public Boolean isPub() { return pub;} } I can use item.getType() to get the type value, but I'm looking for a generic way, say I have a string 'Type', then I'd like to get the value of type of a certain object, any idea how to achieve this? Thanks. A.C.
From: Daniel Pitts on 12 Mar 2010 11:25 On 3/12/2010 6:02 AM, angelochen960(a)gmail.com wrote: > Hi, > > I have this bean: > > public class Item { > private String type; > private Boolean pub; This should probably be a "boolean", not a Boolean. > > public String getType() { return type;} > > public Boolean isPub() { return pub;} Same as above. Boolean is an object reference type, which may end up being null. Most of the time, you don't want a null for a boolean value. This is true of most of the primitive types. > } > > > I can use item.getType() to get the type value, but I'm looking for a > generic way, say I have a string 'Type', then I'd like to get the > value of type of a certain object, any idea how to achieve this? > Thanks. > > A.C. Actually, it would be if you had the string "type", because that is the name of the property. You can use introspection, or use a third-party library (which uses reflection/introspection). Spring has some good classes for this. I often prefer OGNL for an embedded expression language. So, the Big Question is, what are you *really* trying to do? If you have to ask about reflection, then you are hopefully not working on a production system. Reflection is useful for framework-type development, but developing a useful and maintainable framework requires experience. If you're playing around with reflection for fun and/or homework, then have at it. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Arne Vajhøj on 12 Mar 2010 20:29 On 12-03-2010 11:25, Daniel Pitts wrote: > On 3/12/2010 6:02 AM, angelochen960(a)gmail.com wrote: >> I have this bean: >> >> public class Item { >> private String type; >> private Boolean pub; > This should probably be a "boolean", not a Boolean. >> >> public String getType() { return type;} >> >> public Boolean isPub() { return pub;} > Same as above. Boolean is an object reference type, which may end up > being null. Most of the time, you don't want a null for a boolean value. > This is true of most of the primitive types. Data classes frequently have the requirement to support null for data not available or not applicable. Arne
From: Daniel Pitts on 12 Mar 2010 20:52 On 3/12/2010 5:29 PM, Arne Vajh�j wrote: > On 12-03-2010 11:25, Daniel Pitts wrote: >> On 3/12/2010 6:02 AM, angelochen960(a)gmail.com wrote: >>> I have this bean: >>> >>> public class Item { >>> private String type; >>> private Boolean pub; >> This should probably be a "boolean", not a Boolean. >>> >>> public String getType() { return type;} >>> >>> public Boolean isPub() { return pub;} >> Same as above. Boolean is an object reference type, which may end up >> being null. Most of the time, you don't want a null for a boolean value. >> This is true of most of the primitive types. > > Data classes frequently have the requirement to support > null for data not available or not applicable. Which is why I used the phrase "Most of the time" as opposed to "All of the time". In my experience, it is more common for someone mistakenly choose a wrapper than for someone meaningfully choose a wrapper. It is also less common that someone mistakenly chooses a primitive over a wrapper. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Arne Vajhøj on 16 Mar 2010 21:56
On 12-03-2010 20:52, Daniel Pitts wrote: > On 3/12/2010 5:29 PM, Arne Vajh�j wrote: >> On 12-03-2010 11:25, Daniel Pitts wrote: >>> On 3/12/2010 6:02 AM, angelochen960(a)gmail.com wrote: >>>> I have this bean: >>>> >>>> public class Item { >>>> private String type; >>>> private Boolean pub; >>> This should probably be a "boolean", not a Boolean. >>>> >>>> public String getType() { return type;} >>>> >>>> public Boolean isPub() { return pub;} >>> Same as above. Boolean is an object reference type, which may end up >>> being null. Most of the time, you don't want a null for a boolean value. >>> This is true of most of the primitive types. >> >> Data classes frequently have the requirement to support >> null for data not available or not applicable. > Which is why I used the phrase "Most of the time" as opposed to "All of > the time". > > In my experience, it is more common for someone mistakenly choose a > wrapper than for someone meaningfully choose a wrapper. It is also less > common that someone mistakenly chooses a primitive over a wrapper. Data classes and CRUD are a big part of IT. And choosing a wrapper unnecessary have very small consequences while mistakenly choosing a primitive is a real data integrity problem. Arne |