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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:41:20+00:00 2026-06-13T22:41:20+00:00

I wondered if it was possible to create a templatized getter, such as :

  • 0

I wondered if it was possible to create a “templatized” getter, such as :

class TestGet
{
    private static Map<String,Object> theMap.....

    public static getObject<T>(String key).....

And somewhere in the code, something like :

JButton myButton = TestGet.getObject<JButton>("theButton");

Beware : this is just an “idle” question, I’m trying to avoid “dirty” casts in my code since they make my eyes bleed each time I look at them.

I know there are more elegant ways to do that, but I really would do it with a map of to avoid linking with other classes.

I tried out some configurations, but nothing seems to fit what I want to do… Is a templating like that just possible ?

  • 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-13T22:41:21+00:00Added an answer on June 13, 2026 at 10:41 pm

    You can write

     public static <T> T getObject(String key) {
         return (T) theMap.get(key); // produces a warning.
     }
    

    and

     JButton myButton = TestGet.<JButton>getObject("theButton");
    

    or just

     JButton myButton = TestGet.getObject("theButton");
    

    For a more complicated example 😉

    class A<T> {
        class B<S> {
            public <U extends S> U tsu() {
                return null;
            }
        }
    }
    
    List<String> list = new A<Integer>().new B<List>().<List<String>>tsu();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to MDX and wondered if it is possible to create a
I just wondered if it would be possible to create a small, simple jpg,
I've been evaluating django and wondered if the following is possible. I've already looked
I'm wondered is it possible to debug .net embedded js files in WebStorm? Because,
I wondered about if it's possible to add two or more complete functions to
I just wondered whether it's possible to match against the same values for multiple
I have using .on() in jQuery 1.7 and wondered whether it is possible to
I can set up an autofilter using pyWin32, but I wondered if it's possible
Is it possible to do string negation in regular expressions? I need to match
Is it possible to create multiple jobs with in a single thread? I am

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.