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 58199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:48:38+00:00 2026-05-10T17:48:38+00:00

I have the following code that won’t compile and although there is a way

  • 0

I have the following code that won’t compile and although there is a way to make it compile I want to understand why it isn’t compiling. Can someone enlighten me as to specifically why I get the error message I will post at the end please?

public class Test {     public static void main(String args[]) {         Test t = new Test();         t.testT(null);     }      public <T extends Test> void testT(Class<T> type) {         Class<T> testType = type == null ? Test.class : type; //Error here         System.out.println(testType);     } } 

Type mismatch: cannot convert from Class<capture#1-of ? extends Test> to Class<T>

By casting Test.class to Class<T> this compiles with an Unchecked cast warning and runs perfectly.

  • 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-10T17:48:38+00:00Added an answer on May 10, 2026 at 5:48 pm

    The reason is that Test.class is of the type Class<Test>. You cannot assign a reference of type Class<Test> to a variable of type Class<T> as they are not the same thing. This, however, works:

    Class<? extends Test> testType = type == null ? Test.class : type; 

    The wildcard allows both Class<T> and Class<Test> references to be assigned to testType.

    There is a ton of information about Java generics behavior at Angelika Langer Java Generics FAQ. I’ll provide an example based on some of the information there that uses the Number class heirarchy Java’s core API.

    Consider the following method:

    public <T extends Number> void testNumber(final Class<T> type) 

    This is to allow for the following statements to be successfully compile:

    testNumber(Integer.class); testNumber(Number.class); 

    But the following won’t compile:

    testNumber(String.class); 

    Now consider these statements:

    Class<Number> numberClass = Number.class; Class<Integer> integerClass = numberClass; 

    The second line fails to compile and produces this error Type mismatch: cannot convert from Class<Number> to Class<Integer>. But Integer extends Number, so why does it fail? Look at these next two statements to see why:

    Number anumber = new Long(0); Integer another = anumber; 

    It is pretty easy to see why the 2nd line doesn’t compile here. You can’t assign an instance of Number to a variable of type Integer because there is no way to guarantee that the Number instance is of a compatible type. In this example the Number is actually a Long, which certainly can’t be assigned to an Integer. In fact, the error is also a type mismatch: Type mismatch: cannot convert from Number to Integer.

    The rule is that an instance cannot be assigned to a variable that is a subclass of the type of the instance as there is no guarantee that is is compatible.

    Generics behave in a similar manner. In the generic method signature, T is just a placeholder to indicate what the method allows to the compiler. When the compiler encounters testNumber(Integer.class) it essentially replaces T with Integer.

    Wildcards add additional flexibility, as the following will compile:

    Class<? extends Number> wildcard = numberClass; 

    Since Class<? extends Number> indicates any type that is a Number or a subclass of Number this is perfectly legal and potentially useful in many circumstances.

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

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • 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
  • added an answer If the script files were all loaded in the <head>… May 11, 2026 at 11:05 am
  • added an answer The resulting page is blank when there's an error. One… May 11, 2026 at 11:05 am
  • added an answer You could use string.Join: MessageBox.Show('Found these: ' + string.Join(', ',… May 11, 2026 at 11:05 am

Related Questions

I have the following code that won't compile and although there is a way
I have the following code that shows either a bug or a misunderstanding on
I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have the following code that I need to add an additonal object to
I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call
I have the following html.erb code that I'm looking to move to Haml: <span
I have the following snippet of code that's generating the Use new keyword if
I have the following code in an Autofac Module that is used in my
I have the following bit of legacy C++ code that does not compile: #include

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.