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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:18:01+00:00 2026-05-30T07:18:01+00:00

I understand generics when it comes to collections. But what does it mean in

  • 0

I understand generics when it comes to collections. But what does it mean in the case of the Class<T> class? When you instantiate a Class object, there’s only one object. So why the T parameter? What is it specifying? And why is it necessary (if it is)?

  • 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-30T07:18:03+00:00Added an answer on May 30, 2026 at 7:18 am

    Type parameter <T> has been added to java.lang.Class to enable one specific idiom1 – use of Class objects as type-safe object factories. Essentially, the addition of <T> lets you instantiate classes in a type-safe manner, like this:

    T instance = myClass.newInstance();
    

    Type parameter <T> represents the class itself, enabling you to avoid unpleasant effects of type erasure by storing Class<T> in a generic class or passing it in as a parameter to a generic method. Note that T by itself would not be sufficient to complete this task2: the type of T is erased, so it becomes java.lang.Object under the hood.

    Here is a classic example where <T> parameter of the class becomes important. In the example below, Java compiler is able to ensure type safety, letting you produce a typed collection from a SQL string and an instance of Class<T>. Note that the class is used as a factory, and that its type safety can be verified at compile time:

    public static <T> Collection<T> select(Class<T> c, String sqlStatement) {
        Collection<T> result = new ArrayList<T>();
        /* run sql query using jdbc */
        for ( /* iterate over jdbc results */ ) {
            T item = c.newInstance();
            /* use reflection and set all of item’s fields from sql results */
            result.add(item);
        }
        return result;
    }
    

    Since Java erases the type parameter, making it a java.lang.Object or a class specified as the generic’s upper bound, it is important to have access to the Class<T> object inside the select method. Since newInstance returns an object of type <T>, the compiler can perform type checking, eliminating a cast.


    1 SUN Oracle has published a good article explaining all this.
    2 This is different from implementations of generics without type erasure, such as one in .NET.
    3 Java Generics tutorial by Oracle.

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

Sidebar

Related Questions

I've never used any of the .NET generics in my work, but I understand
I learned java generics some time ago, but now I'm learning collections and found
I am unable to understand what the generics code below does. I am new
I am just trying to understand the extends keyword in Java Generics. List<? extends
I'm trying to understand type patterns and generic classes in Haskell but can't seem
I know this is a bit generic, but I'm sure you'll understand my explanation.
I thought I understood Java generics pretty well, but then I came across the
I don't think I really understand Java generics. What's the difference between these two
I am going through a book trying to understand Generics with C# and I
I'm trying to extend an abstract class with generics and I'm running into a

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.