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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:35:59+00:00 2026-06-17T09:35:59+00:00

Well the question says it all. Heres an Example of what i want String

  • 0

Well the question says it all. Heres an Example of what i want

String s = new String();
List<s.type> ls = new ArrayList<s.type>();

Something like that, or any elegant way to achieve this, please no multiple if conditions or condition logic, as there may be unlimited data types, i have used string here only as an example, it can be anything.

  • 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-06-17T09:35:59+00:00Added an answer on June 17, 2026 at 9:35 am

    If I understood you correctly, you want to know the class (type) of an instance (object) in runtime (existing), which has nothing to do with generics though, as Petr explained.

    public class Types {    
     public static void main(String[] args) {
         String s = "";
         Runtime r = Runtime.getRuntime();
         Types t = new Types();
    
         List<Class> types = new LinkedList<Class>();         
         types.add(Integer.class);
         types.add(t.getClass());
         types.add(r.getClass());
         types.add(s.getClass());
    
         for (Class type :types) {
             System.out.println(type.getSimpleName());
         }         
     }    
    }
    

    which outputs:

    Integer
    Types
    Runtime
    String
    

    However, such approach is rarely needed, so rethink is there a simpler solution to your problem. If you still want to tackle it like this:

    • What is reflection, and why is it useful?
    • official tutorial

    EDIT:

    The other interpretation of your question leads to a dynamic generic list as answer:

    public static <T> List<T> createListOfType() {
        return new ArrayList<T>();
    }
    
    public static void main(String[] args) {
        List<String> a = createListOfType();    
        List<Integer> b = createListOfType();
        a.add("");
        b.add(2);
        a.add(2); // remove this line to compile
        b.add(""); // remove this line to compile       
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, the question says it all. What I would like to do is that,
well the question says it all I have some contour and I want to
well, I guess the question says it all I'm open to using Private API's,
Well the main question says it all, why are the files loaded via https.
Well the question says it all.I am trying to create a file transfer application
Well the question says it all? I have a DataTemplate with a button is
As well as my question Removing MKMapView Annotations causes leaks. I have discovered that
Here's one from the No question's too dumb department: Well, as the subject says:
I want to retrieve the recently viewed pictures from IE. I know that all
In the chosen answer for this question about Blue Ruby , Chuck says: All

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.