Results 1 to 6 of 6
- 04-26-2011, 03:41 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
How do I specify a generic for an instance field?
How do I specify a generic for an instance field? What is the correct syntax?
For example how do I make an instance field of type T where T extends Sprite? I'm thinking something like the following but it doesn't seem to work:
private <T extends Sprite> Collection<T> someName;
For methods I can just write the following
public <T extends Sprite> void foo(Collection<T> param)...
but I'm not sure how to do this for a field. My class is not generic in itself and I do not wish it to be.
- 04-26-2011, 04:40 PM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
I would have it simply be a collection of Sprite.
Java Code:private Collection<Sprite> someName;
- 04-26-2011, 07:44 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
Generic instance fields
How do I specify a generic for an instance field? What is the correct syntax?
For example how do I make an instance field of type T where T extends Sprite? I'm thinking something like the following but it doesn't seem to work:
private <T extends Sprite> Collection<T> someName;
For methods I can just write the following
public <T extends Sprite> void foo(Collection<T> param)...
but I'm not sure how to do this for a field. My class is not generic in itself and I do not wish it to be.
- 04-27-2011, 06:47 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
I don't believe you can do this unless the class is generic. With generic methods, the compiler can work out the type T from the arguments used, but for a member field, it has nothing to go on, unless T has been specified in the class instantiation.
If you explain what you are trying to achieve, we may be able to find an alternative approach.
- 04-27-2011, 08:13 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
In your case, it would be:
private Collection<? extends Sprite> someName;
-
Moderator action: Two threads merged. Origin poster, please don't double post your questions.
Similar Threads
-
front end display of field description when clicking the field name
By neils in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 10-29-2010, 11:47 AM -
How to check instance of a generic class?
By chan_nguyen in forum New To JavaReplies: 7Last Post: 09-08-2010, 04:20 AM -
Find instance that owns instance?
By Addez in forum New To JavaReplies: 2Last Post: 04-04-2010, 03:36 PM -
how from an Access Currency field I populate a hidden field
By lse123 in forum Java ServletReplies: 4Last Post: 01-17-2010, 11:13 PM -
Constructor and instance field
By MIA6 in forum New To JavaReplies: 7Last Post: 11-02-2009, 06:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks