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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:42:27+00:00 2026-06-05T13:42:27+00:00

I am new to generics. Having a Map like private static Map<String, Object> map;

  • 0

I am new to generics.

Having a Map like

private static Map<String, Object> map;

and a method like

public <T> T getObject(final Class<T> myClass) {
    return (T)map.get(myClass);
}

How to change the map declaration in order to not have to do the cast when returning from the method ?

  • 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-05T13:42:28+00:00Added an answer on June 5, 2026 at 1:42 pm

    You would need to make a generic class, not a generic method:

    public class MyClass<T> {
       private Map<String, T> map;
    
       public T getObject(final String key) {
        return map.get(key);
       }
    }
    

    Also, I changed the parameter from a Class to a String. It doesn’t make sense to pass a Class if map.get() expects a String.

    Edit: I didn’t notice that map was static. If you can change it to non-static without it breaking other parts of your program, this could work. If you can’t, then you cannot avoid a cast.

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

Sidebar

Related Questions

Using Java Generics, I tried to implement a generic console input method. public static
Let's assume the following method (say from Guava's Iterables): public static <T> Iterable<T> filter(final
Is it possible to use generics with the IL Generator? DynamicMethod method = new
I have a Generic List as below public static readonly List<Customer> Customers = new
I am working on a class library and am having some trouble with generics.
I'm fairly new to generics and I'm having some trouble understanding parts of how
I'm having a bit of trouble writing a class that uses generics because this
I am creating an object cache. I am having trouble writing a method return
I’d like to declare a generic class Simple where a method called Addition would
private ArrayList<String> colors = new ArrayList<String>(); Looking at the example above, it seems the

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.