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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:52:47+00:00 2026-06-14T12:52:47+00:00

So say I have: public class Foo<T> { // Some stuff } public interface

  • 0

So say I have:

public class Foo<T> {
  // Some stuff
}

public interface Factory<T> {
  T newInstance();
  Class<T> getKlass();
}

And I want to make an implementation of Factory to build Foo<T>s:

public class FooFactory<T> implements Factor<Foo<T>> {
  private Class<Foo<T>> klass;
  public FooFactory(Class<Foo<T>> klass) {
    this.klass = klass;
  }

  public T newInstance() throws Exception {
    return klass.newInstance();
  }

  public Class<Foo<T>> getKlass() {
    return klass;
  }
}

Two questions:

  1. Is there any way for me to get a Class<Foo<T>> if all I have is Class<T>? Unfortunately Foo is a private class, and therefore the only code that has the whole picture (i.e., knowledge of what T will be) can’t know about Foo and therefore can’t give me Class<Foo<T>>.
  2. Is there a better way I could be going about this?

Thanks!

P.S. For those asking for more detail, here we go. The full story is that Foo and FooFactory will both be private classes in an outer class. Foo is used extensively throughout the outer class, and to avoid spurious garbage collection, I’d like to use an ObjectPool to instantiate instances of Foo rather than newing them up and having them garbage collected all the time. However, in order to use my ObjectPool, I need a Factory<Foo<T>>, and inside of Outer<T> I obviously can’t know what T will be when Outer<T> is instantiated.

public class Outer<T> {
  private class Foo<T> { ... }
  private class FooFactory<T> {...}

  private ObjectPool<Foo<T>> fooPool;

  ...
}
  • 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-14T12:52:48+00:00Added an answer on June 14, 2026 at 12:52 pm

    There are ways to create a the type information for Class<Foo<T>> but to do what you want, you simply need to suppress all generics warnings in FooFactory.

    At runtime, Java uses Object in all places where you wrote T, so you can simply instantiate Foo<Object> or Foo.class, they are the same. This is called type erasure.

    Generics are just a hint for the compiler. Since your API makes sure that all code using FooFactory will honor the type T, you can suppress the warnings and use the appropriate casts and it will work.

    Note: If you want to preserve type information, implement the ParameterizedType interface.

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

Sidebar

Related Questions

Say I have some class, e.g. Foo: public class Foo { private Integer x;
Say I have some code like this: class Foo { public: Foo(int v) :
Let's say we have some Foo class that contains public void setOnSomethingListener(OnSomethingListener listener); We
Let's say I have a regular simple Java class, such as: public class Foo
Say I have the following class: class Foo { // ctor etc here public
Lets say we have a simple class public class Foo { public string FooName;
Let's say I have a simple POJO class: public void Foo { public int
Say I have the following: public class A { //stuff public class B implements
Let's say I have the following code: public class Foo { private int x;
Say I have a class with three string properties: public class Foo { public

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.