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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:13:22+00:00 2026-05-22T20:13:22+00:00

How do I call Class.forName() when the result is a generic type? Usually I

  • 0

How do I call Class.forName() when the result is a generic type? Usually I can use asSubclass(), but here the only way I see to do it is a cast, which kindof sticks out & bugs me when everything else is nicely typed with generics.

The scenario goes something like this:

There is a .jar with one entry point main class that has a main(). It takes an option of a classname (and some others, irrelevant here). The class given implements Callable<Integer>. This class is loaded, inited & launched.

Here is an example of what I need:

Class<? extends Callable<Integer>> clazz = (Class<? extends Callable<Integer>>) Class.forName(options.valueOf(className)).asSubclass(Callable.class);

Is there any way to get rid of that cast?

Using SE6.

  • 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-22T20:13:23+00:00Added an answer on May 22, 2026 at 8:13 pm

    First you probably want a full generic Class

    Class<Callable<Integer>> classCI = ...;
    

    Then the java type system has no problem with

    Class<? extends Callable<Integer>> clazz =     
        Class.forName(options.valueOf(className))
        .asSubclass(classCI);
    

    How can we get classCI? We can cheat by unchecked cast

    Class<Callable<Integer>> classCI = (Class<Callable<Integer>>)Callable.class;
    

    This is inherently unsafe. There must be external forces to make sure the className really is a Callable<Integer>. For example if it’s a Callable<String>, the program runs through all the casts without any problem, and it only blows up much later when Integer call() is invoked, and the error message will be very misleading.

    It’s ok if a cast cannot be analyzed statically to succeed:

    Object o = ...;
    String s1 = (String)o; // may fail, no javac warning
    String s2 = String.class.cast(o); // may fail, no javac warning
    

    as long as an exception is immediately thrown when the cast fails at runtime.

    To be type safe, we must proactively check the generic type of the className

    @SuppressWarning( "unchecked" )
    Class<? Callable<Integer>> getClass(String className)
    {
        Class clazz = Class.forName(className);
        via reflection, check generic super interfaces of clazz
        if there's no Callable<Integer> super interface
            throw "className is not a Callable<Integer>"
    
        // we have *checked*, the following cast is safe
        return (Class<? Callable<Integer>>)clazz; 
    }
    

    We are justified to suppress “unchecked” here, because the implementation checks to make sure that if the className doesn’t really denote a class implementing Callable<Integer>, it immediately throws an exception right there. Our cast is “checked”, and the program is type safe.

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

Sidebar

Related Questions

Given the following code, is there a way I can call class A's version
So here is the deal. I want to call a class and pass a
For example when I have a class named; 'MonkeyBusiness' I know I can call
Is there a way in Python to call class object from function definition ?
I have to create a class dynamically but I want to use class constructor
How to call from class oneThread: back to class fun:? As in, address a
Say you have a custom class call it Foo. When you then have an
What does it mean to call a class like this: class Example { public:
Suppose we are writing a class (let's call it Class) in an iPhone program.
In Visual Studio 2008 (C++) I have a class destructor (Let's call this class

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.