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

The Archive Base Latest Questions

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

Assuming T as a class type parameter, why cannot I use T.class I was

  • 0

Assuming T as a class type parameter, why cannot I use T.class

I was writing a function that download a page and parses it according to a passed class.
For parsing, I use another function whose signature is : ParseObject::parse(Class<T> classname)

<T> void downloadParse(){
  ParseObject obj;
  obj.parse(T.class); //<--- why compiler error here?? (whereas something like Integer.class is allowed)
}
  • 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-26T21:25:13+00:00Added an answer on May 26, 2026 at 9:25 pm

    Java generics are implemented via type erasure. They can only be used for compile time checking. After compiliation, the object gets changed to the lowest common object. (In this case Object.class).

    The compiled bytecode has no idea what T is.

    If you want access to the class, you need to change the method to:

    <T> void downloadParse(Class<T> cls){
      ParserObject obj;
      obj.parse(cls);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming the following class stub: public class Foo<T> { private Class<T> type; public Foo<T>
I'm trying to implement a class that stores a generic nullable type: public class
Assuming that I have the following interface and class: public interface IFooRepo : IDisposable
Assuming that I have a class named Class , And I would like to
Assuming a base type of Message... Public Class Message Public ID As Int64 Public
Assuming I have a simple class that represents a staff member class Staff {
Assuming a class called Bar in a namespace called foo , which syntax do
Assuming public class MyClass { public int ID {get; set; } public string Name
Assuming I have only the class name of a generic as a string in
Assuming I have a class A as follows: class A{ int id; int getId(){};

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.