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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:07:38+00:00 2026-05-21T12:07:38+00:00

I have a Generic Class Factory class that has two methods one utilizes the

  • 0

I have a Generic Class Factory class that has two methods one utilizes the Class generic T value and the other only uses its own method generic definitions.

public class GenericClassFactory<T extends ClassMatchable> {
    public <E, K> E newObject(ClassMatcher<E, K> matcher, K key, String packageName){...}
    public <K> T newObject(K key, String packageName){...}
}

The method that utilizes the T generic works fine but when I want to use the other method that doesn’t care what the T generic is it won’t use the Generic E it will just return an Object and then I have to type cast it.

Data data = new GenericClassFactory().newObject(new ClassMatcher<Data, String>(){...}, "key1", "my.package.name.impl");

This has compile errors because it wants me to typecast it to (Data). If I pass the GenericClassFactory a valid Class Generic it will work. Its like it doesn’t recognize method generics if you have a Class Generic defined but not used.

Data data = new GenericClassFactory<ClassMatchable>().newObject(new ClassMatcher<Data, String>(){...}, "key1", "my.package.name.impl");

That works fine. But it’s dumb that I would have to define a class generic like that when it isn’t needed for my purposes. I could do this:

public class GenericClassFactory {
    public <E, K> E newObject(ClassMatcher<E, K> matcher, K key, String packageName){...}
    public <T extends ClassMatchable, K> T newObject(K key, String packageName){...}
}

But now my second method seems like its too broad or something…maybe not. I mean it will still give a compile error if the object you are assigning to the return type doesn’t implement ClassMatchable. Is that the way I should go? So that I don’t have to typecast?

  • 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-21T12:07:39+00:00Added an answer on May 21, 2026 at 12:07 pm

    That’s right, if you don’t type a class reference, then even generic methods that only use method type parameters will not be generified. It’s one of the weirder nuances of Java Generics. As you say, you can put in some arbitrary type for T:

    Data data = new GenericClassFactory<ClassMatchable>().newObject(new ClassMatcher<Data, String>(){...}, "key1", "my.package.name.impl");
    

    But more likely this shouldn’t even be an instance method. Can’t it be a static method? If so you could just invoke it like this:

    Data data =  GenericClassFactory.newObject(new ClassMatcher<Data, String>(){...}, "key1", "my.package.name.impl");
    

    Edit

    Note that this extends to all instance members, not just generic instance methods. Thus, there are simpler cases that demonstrate this odd nuance. This code compiles with only warnings:

    public class Scratchpad<T> {
       List<String> list;
       public static void main(String[] args) {
          Scratchpad sp = new Scratchpad();
          List<Integer> list = sp.list;
       }
    }
    

    And that’s because sp.list is resolved as a List, not a List<String>, even though Scratchpad.list has nothing to do with T.

    This is verbosely documented in the JLS, Section 4.8:

    The type of a constructor (§8.8), instance method (§8.8, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the erasure of its type in the generic declaration corresponding to C. The type of a static member of a raw type C is the same as its type in the generic declaration corresponding to C.

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

Sidebar

Related Questions

I want to have a generic class that implements Iterable (let's call it ImplIterable)
I have a generic class defined like this class MyGenericClass<T> where T : ICompareable
I have a generic class implementing IValueConverter . Something like: class MyValueConverter<T> : IValueConverter
I have a generic class (C#), class MyClass<T> where T : struct, IComparable<T> {
For an Android project I have a number of response tasks that I want
I have posted a question on here previously asking similar advise, but this project
I'm creating a reusable library for Silverlight. The library contains an internal generic type
I don't see why I have to create a DbCommand object every time I
I'm out of ideas how to configure right Windsor container for use with repositories

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.