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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:08:22+00:00 2026-06-16T21:08:22+00:00

I have a slightly odd issue with Java generics. Actually it might not be

  • 0

I have a slightly odd issue with Java generics. Actually it might not be an issue at all and it might just me that I am trying to use generics incorrectly in design. I will not give the entire details of the application (it is irrelevant and boring).

I have created a small snippet that captures the essence of the problem.

There is a Producer interface which looks like this.

public interface Producer<P> {
public P produce(); 
}

There is an implementation of this interface which looks like this

public class IntegerProducer<P> implements Producer<P> {

@Override
public P produce() {
    return (P)new Integer(10);
}
}

What I really need to do in the produce method is something like

if (P instanceOf Integer) return (P)new Integer(10);
else throw new Exception("Something weird happened in the client app"); 

Now of course the P instanceOf Integer won’t work. But if you have got the essence of what I am trying to do, would you please share a solution.

Thanks for helping.

Clarification 1:
The issue is not that I want to return Integer only. The issue is that in the function produce(), I need to check the type of generics used by the client program and change the behaviour of the function based on that. It is not that I want to restrict the type of generics used to a particular type of Object (where I could have used wildcards) but I need the function produce() to behave slightly differently based on the kind of Object used in the generics by the client code.

  • 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-16T21:08:23+00:00Added an answer on June 16, 2026 at 9:08 pm

    How about:

    public class IntegerProducer implements Producer<Integer> {
    
        @Override
        public Integer produce() {
            return 10;
        }
    }
    

    Since your IntegerProducer deals with Integers only, you don’t need it to be generic. You can then write:

    Producer<Integer> p = new IntegerProducer();
    Integer i = p.produce();
    

    EDIT

    Following your comment, I think the only way would be to pass a class parameter:

    public class Producer<T> {
    
        public static void main(String[] args) throws InterruptedException {
            Producer<Integer> t1 = new Producer<> ();
            Producer<String> t2 = new Producer<> ();
            System.out.println(t1.get(Integer.class)); //prints 10
            System.out.println(t1.get(String.class)); //does not compile
            System.out.println(t2.get(String.class)); //throws exception
        }
    
        public T get(Class<T> c) {
            if (c == Integer.class) {
                return (T) (Object) 10;
            }
            throw new UnsupportedOperationException();
        }
    }
    

    Alternatively you can pass it in the constructor (strategy used for example by EnumSets and EnumMaps):

    public class Producer<T> {
    
        public static void main(String[] args) throws InterruptedException {
            Producer<Integer> t1 = new Producer<> (Integer.class);
            Producer<Integer> t1 = new Producer<> (String.class); //does not compile
            Producer<String> t2 = new Producer<> (String.class);
            System.out.println(t1.get()); //prints 10
            System.out.println(t2.get()); //throws exception
        }
    
        private final Class<T> c;
    
        public Producer(Class<T> c) {
            this.c = c;
        }
    
        public T get() {
            if (c == Integer.class) {
                return (T) (Object) 10;
            }
            throw new UnsupportedOperationException();
        }
    }
    

    That clearly clutters the code and I would personally reconsider the design to avoid that kind of situation.

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

Sidebar

Related Questions

A slightly odd question for you all - I have solved my issue of
I have some queries being run in a java program that makes use of
I have a slightly odd problem that I think is most likely due to
I'm trying to use Rails's polymorphic associations in a slightly odd way and I'm
I have a slightly odd situation. One of my customers has an Asp.net (v2
I have a slightly theoretical question that I want to clear up before I
I have a need for two slightly different classes, that have the same members,
Here's a SQL query that I slightly scrubbed (just the column/table/database names). I kept
I'm having an odd issue with CALayer drawing for the iPhone. I have a
slightly odd scenario. I have RequiresQuestionAndAnswer set to false because the admin needs to

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.