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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:34:27+00:00 2026-06-07T23:34:27+00:00

I am testing some small practicals with JAVA generics concept. I try to return

  • 0

I am testing some small practicals with JAVA generics concept.

I try to return a List from a function that is generic but compile does not allow me to do so.

Check the code below :

package com.test.generic.method;

import java.util.ArrayList;
import java.util.List;


public class Sample_3<T> {

    public <T> List<T> testReturn(T t) {
        List<T> list = new ArrayList<T> ();
        list.add(t);
        return list;
    }

    public static void main(String a[]) {
        String s = "Gunjan";

        Sample_3 sample = new Sample_3<String>();
        List<String> list =(List<String>) sample.testReturn(sample);


        for(String ab : list){
            System.out.println(ab);
        }

    }
}

It gives ClassCastException.

How can I return the list from the generic function ?
And why JAVA has added such compile time feature ?

Thanks,
Gunjan.

  • 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-07T23:34:29+00:00Added an answer on June 7, 2026 at 11:34 pm

    The purpose of the generics is to allow you freedom to specify. So you can do something like:

    public List<T> testReturn(T t){
            List<T> list = new ArrayList<T>();
            list.add(t); 
            return list; 
        }
    

    which would then allow you to do e.g.

    List<String> a = testReturn("A");
    List<Integer> b = testReturn(1);
    

    The issue with your code is that the intent of your method isn’t really generic – the method is written to return a List<String>. The <T> in your code doesn’t actually affect anything.

    In response to your further question –
    You’ve written the following:

    public class Sample_3 {
       public <T> List<T> testReturn(T t) {
        List<T> list = new ArrayList<T> ();
        list.add(t);
        return list;
       }
    
       public static void main(String a[]) {
        String s = "Gunjan";
        // this is wrong - the Sample_3 class is not generic. It does not get a generic type parameter.
        Sample_3 sample = new Sample_3<String>();
        // this is also wrong. Since the method is generic the "T" you get is the same as the "T" that 
        // goes into the method. Here your String output type does not match the input type of the method,
        // which you've set here as a Sample_3 object
        List<String> list =(List<String>) sample.testReturn(sample);
    
    }
    

    Your generic parameter T sets the type that can vary. YOu can do it onthe method level, as above, or on the class level as well:

    public class Sample_3<T> {
       public List<T> getList(){ ... }
    }
    

    e.g.

     List<Integer> l = new Sample_3<Integer>().getList();
     List<String> s = new Sample_3<String>().getList();
     List<Calendar> c = new Sample_3<Calendar>().getList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a side result of testing some code I wrote a small function to
I am testing some small scripts that I have. This particular one is driving
I'm testing some methods that involve email, and am trying to use a mock
We are testing some code to send email messages using Gmail from a form,
I'm learning and testing some graph libraries and am running into a weird problem(but
I'm testing some cookies that I'm creating via JavaScript. Is there a way to
I am writing a small glue tool for doing some testing and I would
I have created an additional thread in some small testing app and want to
I have a Java library that uses a few things from the Android APIs.
I'm currently doing some testing work with TOR and ran in to a small

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.