Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:59:25+00:00 2026-05-26T06:59:25+00:00

I am trying to implement parameterized class in Java with enum as a type

  • 0

I am trying to implement parameterized class in Java with enum as a type parameter. Everything works fine, except, if you look at the code below, there is that anonymous class Car.Creator with parameter K. But of course, instead of K, there should be CarObject<T>, but it’s not that easy. If I put CarObject<T> in K‘s place, then I got syntax error. Could someone explain if something like this is possible and maybe provide with some code sample.

public class CarObject<T extends Enum<T>>
{
  public Map<T, String> values;
  private Class<T> typeClass;

  public CarObject(Class<T> clazz)
  {
    typeClass = clazz;
    values = new EnumMap<T,String>(typeClass);
  }


  public static final Car.Creator<K> Creator = new Car.Creator<K>()
  {
    public K create()
    {
      return new K();
    }


    public K[] newArray(int size)
    {
      return new K[size];
    }
  }
}

I can give you an example from official Android documentation(look at the code in ‘Class Overview’) where this works perfectly fine. I think there is some magic going on under the hood in Android. I’m trying to do exactly the same thing – implement Parcelable interface. I just made up this example without implements Parcelable and other stuff, because I though maybe it’s just a syntax sugar :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:59:25+00:00Added an answer on May 26, 2026 at 6:59 am

    I’m no Android expert, but I’m pretty sure there’s nothing magic about how this is happening in Android. I think you’re just confused about the relationship between generic type arguments and statics.

    Generic type arguments (like T in your example) belong to the instances of the class, not to the class itself.

    Static members (like Creator in your example) belong to the class itself, not to the instances of the class.

    The fact that Creator is an anonymous inner class implementing a generic interface is a bit of a red herring here. The issue is simply that, regardless of what Creator is, as long as it is static, it can’t access the type T.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement an inner class that has a generic parameterized type.
I'm trying to implement in Scala a generic data type parameterized on a type
SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the
I am trying implement the Data transformation using Reflection 1 example in my code.
Trying to implement a function in my linkedlist class that will return total amount
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all
Trying to implement a rating system of users and postings. What is the best
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.