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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:05:11+00:00 2026-05-11T07:05:11+00:00

I have a legacy class that the class itself is not a generic but

  • 0

I have a legacy class that the class itself is not a generic but one of its methods return type uses generics:

public class Thing {     public Collection<String> getStuff() { ... } } 

getStuff() uses generics to return a collection of strings. Therefore I can iterate over getStuff() and there’s no need to cast the elements to a String:

Thing t = new Thing(); for (String s: t.getStuff())  // valid { ... } 

However, if I change Thing itself to be a generic but keep everything else the same:

public class Thing<T> {     public Collection<String> getStuff() { ... } } 

and then keep using the non-generic reference to Thing, getStuff() no longer returns Collection<String> and instead returns a non-typed Collection. Thus the client code does not compile:

Thing t = new Thing(); for (String s: t.getStuff())  // compiler complains that Object can't be cast to String { ... } 

Why is this? What are the workarounds?

My guess is that by using a non-generic reference to a generic class, Java turns off all generics for the entire class. This is pain, because now I’ve broken my client code by making Thing a generic.

Edit: I’m making Thing generic for another method which is not listed in the above example code. My question is educational as to why the above cannot be done.

  • 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. 2026-05-11T07:05:11+00:00Added an answer on May 11, 2026 at 7:05 am

    Ok, take two, I misunderstood your question.

    When you delcare Thing (this is called a raw type) instead of Thing<?> (parameterized type) the Java compiler strips out all generic arguments, even thogh (as in your case) the generic type of the method has nothing to do with the generic type of the class.

    From the (excellent) Java Generics FAQ:

    Can I use a raw type like any other type?

    Methods or constructors of a raw type have the signature that they would have after type erasure.

    This seemingly inocuous and unobtrusive sentence describes the behaviour in question. You’re using Thing as a raw type so the return type is Collection (not Collection<String>) since this is the type after type erasure.

    Confused? Not surprising. Just look at the size of that FAQ. There’s probably about three people on earth who nderstand the full implications of Java Generics. Just consider my favourite declaration from the JDK:

    Enum<T extends Enum<T>>  

    (Theres an explanation of that in the FAQ too).

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

Sidebar

Ask A Question

Stats

  • Questions 144k
  • Answers 144k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Lucene cannot handle leading wildcards, only trailing ones. That is,… May 12, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer This will return an array with one string per block:… May 12, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer Line buffering. Default is flushing after a "\n". If you… May 12, 2026 at 8:44 am

Related Questions

Let's say you have a class library project that has any number of supplemental
So I'm wiring up my first MasterPage, and everything is working great except for
I take care of critical app in my project. It does stuff related to
Is there any built-in C# support for doing an index sort? More Details: I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.