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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:24:31+00:00 2026-05-23T15:24:31+00:00

My code is as follows package com.foo; public class TestComposition { public static void

  • 0

My code is as follows

package com.foo;

public class TestComposition {
    public static void main(String[] args) {
        try {
            Class<Foo> fooClass = 
                    (Class<Foo>) Class.forName("Foo");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

The assignment within the ‘try’ block results in a warning stating

Type safety: Unchecked cast from Class<capture#1-of ?> to 
     Class<Foo>

Why is this?

  • 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-23T15:24:31+00:00Added an answer on May 23, 2026 at 3:24 pm

    Well, to start with let’s be clear where the problem is – it’s in the cast itself. Here’s a shorter example:

    public class Test {    
        public static void main(String[] args) throws Exception {
            Object x = (Class<Test>) Class.forName("Test");
        }
    }
    

    This still has the same problem. The issue is that the cast isn’t actually going to test anything – because the cast will be effectively converted to the raw Class type. For Class<T> it’s slightly more surprising because in reality the object does know the class involved, but consider a similar situation:

    List<?> list = ... get list from somewhere;
    List<String> stringList = (List<String>) list;
    

    That cast isn’t going to check that it’s really a List<String>, because that information is lost due to type erasure.

    Now in your case, there’s actually a rather simpler solution – if you know the class name at compile-time anyway, just use:

    Class<Foo> fooClass = Foo.class;
    

    If you can provide a more realistic example where that’s not the case, we can help you determine the most appropriate alternative.

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

Sidebar

Related Questions

The code that I am presently using is as follows: My main class public
The code under test follows. view.QueryResultsGrid is a System.Windows.Forms.DataGridView object: public void SelectCheckedChanged(object sender,
For some code as follows, opts, args = getopt.getopt(sys.argv[1:], c:, ... for o,v in
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I have a custom EL function defined as follows: <function> ... <function-signature> java.lang.String getAsText(com.test.Outerclass.InnerEnum)
I have an entity class: Cabbage.java package womble; public class Cabbage { private int
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
I have code as follows: $(#item_select).change(function() { var params = $(#item_select option:selected).val(); $.post('/account/ar_form.php', {idata:
I have 3 different jquery uses & put it into one code as follows:
What advantage, if any, is provided by formatting C code as follows: while(lock_file(lockdir)==0) {

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.