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

  • SEARCH
  • Home
  • 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 79135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:05:54+00:00 2026-05-10T21:05:54+00:00

The following code does not compile: public class GenericsTest { public static void main(String[]

  • 0

The following code does not compile:

public class GenericsTest {         public static void main(String[] args) {         MyList<?> list = new MyList<Object>();         Class<?> clazz = list.get(0);          // Does not compile with reason          // 'Type mismatch: cannot convert from Object to Class'         MyList list2 = new MyList();         Class clazz2 = list2.get(0);     }     static class MyList<T> extends ArrayList<Class<T>> {     } } 

I wanted to do this to introduce generics to old code without breaking the build.

Is this a bug in both the compiler (both eclipse and javac) or am I missing something here? What other possibility exists to introduce generics to MyList?

EDIT

For clarification:

I have the generic class

public class MyList extends ArrayList<MyObject> {} 

with

public class MyObject {} 

and code using MyList

MyList list = new MyList(); ... MyObject o = list.get(0); 

Now during development I see I want to introduce generics to MyObject

public class MyObject<T> {} 

and now I want to have this new generic thingy in MyList as well

public class MyList<T> extends ArrayList<MyObject<T>> {} 

But that does break my build. Interestingly

public class MyList<T> extends ArrayList<MyObject<T>> {     public MyObject<T> get(int i) {         return super.get(i);     } } 

will allow old code

MyList list = new MyList(); ... MyObject o = list.get(0); 

to compile.

OK, seems that when I introduce this generic, I will have to live with having to change all calls to MyList to the generic form. I wanted the old code to just introduce a warning instead of an error.

  • 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. 2026-05-10T21:05:54+00:00Added an answer on May 10, 2026 at 9:05 pm

    I think you are not understanding quite how generics work.

    MyList<?> list = new MyList<Object>(); Class<String> clazz= list.get(0); 

    This code snippet does not compile because you are telling the compiler that list is going to hold Class<Object> types – and then in the next line you are expecting it to return you a Class<String>. The generic system in Java is not capable of converting types used with generics based on inheritance like you might think it would.

    If you expect list to hold Class<String>, then you need to declare it as so – or, if you want it to be able to hold any types, then you cannot do the second line without a cast.

    MyList<String> list = new MyList<String>(); Class<String> clazz = list.get(0); 

    or

    MyList<?> list = new MyList<Object>(); //generates a warning about an unchecked cast Class<String> clazz = (Class<String>) list.get(0); 

    The second code snippet does not work because when you use raw types, you still need to cast the Object returned by get() to the declared type you are using (which has always been the case).

    MyList list2 = new MyList(); Class clazz2 = (Class) list2.get(0); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Change your back slashes to forward slashes and add "file:///"… May 11, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer When should I use an exception? You use an exception… May 11, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer It basically boils down to what level of control you… May 11, 2026 at 7:03 pm

Related Questions

I'm checking out the Delphi 2009 Trial, but run into problems with the generics
This article describes a way, in C#, to allow the addition of arbitrary value
Should the following code give a warning? class Foo { public void Do() {
I'm trying to grasp the concept of .NET Generics and actually use them in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.