Results 1 to 6 of 6
  1. #1
    Nosrettap is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default 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.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I would have it simply be a collection of Sprite.

    Java Code:
    private Collection<Sprite> someName;

  3. #3
    Nosrettap is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default 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.

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    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.

  5. #5
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    In your case, it would be:

    private Collection<? extends Sprite> someName;

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. front end display of field description when clicking the field name
    By neils in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-29-2010, 11:47 AM
  2. How to check instance of a generic class?
    By chan_nguyen in forum New To Java
    Replies: 7
    Last Post: 09-08-2010, 04:20 AM
  3. Find instance that owns instance?
    By Addez in forum New To Java
    Replies: 2
    Last Post: 04-04-2010, 03:36 PM
  4. Replies: 4
    Last Post: 01-17-2010, 11:13 PM
  5. Constructor and instance field
    By MIA6 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 06:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •