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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:59:52+00:00 2026-05-27T23:59:52+00:00

The parameter is ArrayList <T> how can I get the T’s className public static

  • 0

The parameter is ArrayList<T> how can I get the T’s className

public static <T extends Object> void test(ArrayList<T> list){
        T temp;
        Class classType=temp.getClass(); 
        System.out.println(classType.getName());
}

It will be failed to compile that:he local variable temp may not have been initialized.

But how can I get the className of the template class.

  • 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-27T23:59:52+00:00Added an answer on May 27, 2026 at 11:59 pm

    You cannot get the generic type. This is due to how generics are implemented in Java (using type erasure). Basically, you can only use generics to catch type-errors at compile-time.

    Your code fails to compile, because you are trying to call getClass on a local variable that has not been initialized.

    What you could do instead is:

    • pass in a Class<T> parameter in addition to the list to tell the method about the type to be used
    • or look at the first element of the list (if present) and trust that its runtime type is representative enough (which it may not)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The lastIndexOf() method of List interface accepts a parameter which is of type Object.
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
I want to pass a delegate with the return type as ArrayList as parameter
class Json<T> { @SerializedName( T's type here ) private final ArrayList<T> _bucket = new
how can I create report that will iterate over arraylist in detail section that
I have the following class in a common jar: public class Common { public
I have written a simple HelloWorld web service. Which takes an ArrayList as parameter.
I am using GSON 1.4 and serializing an object with two generic arraylist<myObject> as
This example works great for one or the other, but not both: public void
List l = new ArrayList<Number>(); List<String> ls = l; // unchecked warning l.add(0, new

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.