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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:46:26+00:00 2026-05-11T19:46:26+00:00

So I have a map: Map<String, Class> format = new HashMap<String, Class>(); And I

  • 0

So I have a map:

Map<String, Class> format = new HashMap<String, Class>();

And I would add elements to it like this:

format.put("Vendor Number", Integer.class);
format.put("Vendor Dispatch", Date.class); 
....

I have a generic method as follows:

public static <T> T verifyType(String name, Class<T> type) {
    if (type == Integer.class) {
        return type.cast(new Integer(Integer.parseInt(name)));
    }
             ......
    return null;
}

Now this piece of code works great with no compiler issues:

Integer i = verifyType("100",Integer.class);

But, when I try this:

    Integer i = verifyType("100",format.get("Vendor Number"));

OR 

    Class type = Integer.class
    Integer i = verifyType("100",type);

Compiler shows me this warning:
Type safety: Unchecked invocation verifyType(String,Class) of the generic method verifyType(String, Class)

That leaves me puzzled… please help…

  • 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-11T19:46:26+00:00Added an answer on May 11, 2026 at 7:46 pm

    Change:

    Class type = Integer.class
    Integer i = verifyType("100",type);
    

    to

    Class<Integer> type = Integer.class
    Integer i = verifyType("100",type);
    

    By only declaring the type as ‘Class’, you’re losing the generic parameter and the verifyType() method can’t infer the class, thus the unchecked warning.

    This problem:

    Map<String, Class> format = new HashMap<String, Class>();
    format.put("Vendor Number", Integer.class);
    format.put("Vendor Dispatch", Date.class);
    Integer i = verifyType("100",format.get("Vendor Number"));
    

    can’t really be solved due to type erasure. The compiler can’t infer the type based on a generic parameter that is gone by runtime. This is because Java generics are little more than smoke and mirrors for casting.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer An important point (that I didn't realise in the beginning)… May 12, 2026 at 6:51 pm
  • Editorial Team
    Editorial Team added an answer They can do both. With any techlonogy there are good… May 12, 2026 at 6:51 pm
  • Editorial Team
    Editorial Team added an answer The problem with this is if the page itself is… May 12, 2026 at 6:51 pm

Related Questions

I have two entities, Entity1 and Entity2, on a one to many relationship. On
I wrote a file parser for a game I'm writing to make it easy
I have a Map where Coords is defined as so: class Coords { int
So I'm building an App using the Entity Framework on top of SQL Compact

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.