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

  • Home
  • SEARCH
  • 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 156403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:19:22+00:00 2026-05-11T10:19:22+00:00

In this simplified example I have a generic class, and a method that returns

  • 0

In this simplified example I have a generic class, and a method that returns a Map regardless of the type parameter. Why does the compiler wipe out the types on the map when I don’t specify a type on the containing class?

import java.util.Map;  public class MyClass<T> {     public Map<String, String> getMap()     {            return null;     }      public void test()     {            MyClass<Object> success = new MyClass<Object>();         String s = success.getMap().get('');          MyClass unchecked = new MyClass();         Map<String, String> map = unchecked.getMap();  // Unchecked warning, why?         String s2 = map.get('');          MyClass fail = new MyClass();         String s3 = fail.getMap().get('');  // Compiler error, why?     } } 

I get this compiler error.

MyClass.java:20: incompatible types found   : java.lang.Object required: java.lang.String                 String s3 = fail.getMap().get('');  // Compiler error 
  • 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-11T10:19:23+00:00Added an answer on May 11, 2026 at 10:19 am

    Got it. This actually isn’t a bug, strange as it might seem.

    From section 4.8 (raw types) of the JLS:

    The type of a constructor (§8.8), instance method (§8.8, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the erasure of its type in the generic declaration corresponding to C. The type of a static member of a raw type C is the same as its type in the generic declaration corresponding to C.

    So even though the method’s type signature doesn’t use any type parameters of the class itself, type erasure kicks in and the signature becomes effectively

    public Map getMap() 

    In other words, I think you can imagine a raw type as being the same API as the generic type but with all <X> bits removed from everywhere (in the API, not the implementation).

    EDIT: This code:

    MyClass unchecked = new MyClass(); Map<String, String> map = unchecked.getMap();  // Unchecked warning, why? String s2 = map.get(''); 

    compiles because there’s an implicit but unchecked conversion from the raw Map type to Map<String, String>. You can get the same effect by making an explicit conversion (which does nothing at execution time) in the last case:

    // Compiles, but with an unchecked warning String x = ((Map<String, String>)fail.getMap()).get(''); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • 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 Here's what I did. First, there is no way to… May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer It does the same thing as running Regasm.exe with the… May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer No I don't have a JS Framework in place Fix… May 16, 2026 at 8:37 am

Trending Tags

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

Top Members

Related Questions

[This is a simplified example] I have a collection (myCollection) in which exists three
This is a simplified example. I have a canvas and a usercontrol inside it.
Let's say I have a bunch of text like this (simplified example, but you
I have created a stored procedure similar to this simplified example: CREATE PROCEDURE dbo.sp_MyStoredProcedure
<h:commandLink id=#{id} value=#{value} action=#{actionBean.getAction} onclick=alert(this);/> In the previous simplified example, the 'this' keyword used
I ran into some code today that I found questionable. Here's a simplified example
Just created an acc on SO to ask this :) Assuming this simplified example:
I'm curious to know if this is actually possible... In this simplified example, I
I've got another simple one (I think) that's stumping me. I have written a
I have some fairly generic code which uses preprocessor macros to add a certain

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.