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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:42:09+00:00 2026-05-27T07:42:09+00:00

I just learned about this fine looking syntax Collections.<String>emptyList() to get an empty List

  • 0

I just learned about this fine looking syntax

Collections.<String>emptyList()

to get an empty List with elements which are supposedly of type String. Java’s source looks like this:

public static final List EMPTY_LIST = new EmptyList<Object>();
:
public static final <T> List<T> emptyList() {
  return (List<T>) EMPTY_LIST;
}

Now if I code a method in that way where the generic type does not appear in the parameter list, is there any way how I can access the actual class that becomes T?

I’m saying, up to now my approach to code the same thing would have been

private <T> T get(String key, Class<T> clazz) {
  // here I can do whatever I want with clazz, e.g.:
  return clazz.cast(value);
}

If I removed the clazz-parameter I wouldn’t be able to do the cast(). Obviously I could do

  return (T) value;

but that gives me the usual warning Type safety: Unchecked cast from Object to T. Ok, @SuppressWarnings("unchecked") helps here, but actually I want to do something with the intended return type of the method. If I add a local variable

T retValue;

I’d have to initialise it with something, null doesn’t help. After I assign it like

@SuppressWarnings("unchecked")
T retValue = (T) value;

I could do, e.g.

retValue.getClass().getName()

but if the cast fails I end up with no information about T again.

Since Java (or at least my Java 6) does not have the generic info any more during runtime, I currently can’t think of a way to do this. Is there a way? Or do I have to stick with my “old” approach here?

Please note that the example I lined out is very simple and doesn’t make much sense. I want to do more complicated stuff here, but that’s out of the scope.

  • 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-27T07:42:09+00:00Added an answer on May 27, 2026 at 7:42 am

    If you want the generic type at runtime you need to either have it as a field or create a sub-class of a type for a specific combination of types.

    e.g.

    List<String> list = new ArrayList<String>() {}; // creates a generic sub-type
    final Class type = (Class) ((ParameterizedType) list.getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
    System.out.println(type);
    

    prints

    class java.lang.String
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just learned about list comprehension, which is a great fast way to get
I just posted this question and learned about <see cref=> , however when i
I just learned about how the Java Collections Framework implements data structures in linked
I just learned about the AJAX Push Engine but it runs on Linux/Apache which
I just learned about the Template Method pattern in this answer to a question
So I just learned about List Comprehensions in python. some of these are getting
I just learned about character sets today, so forgive the newb factor if this
I just learned about ngrep , a cool program that lets you easily sniff
I just learned about how to include FxCop on a build. But it's slow
I've got an application that just shipped. Since I wrote it, I've learned about

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.