Unchecked Cast Warning when calling 'Class.forName'

前端 未结 4 586
天命终不由人
天命终不由人 2021-01-02 16:52

My code is as follows

package com.foo;

public class TestComposition {
    public static void main(String[] args) {
        try {
            Class

        
4条回答
  •  隐瞒了意图╮
    2021-01-02 17:46

    The runtime system has no knowledge about generics and therfore it can only check that something is of type Class, not if it is of type Class. That is what the warning is about.

    A method has specifically been added to the Class class to avoid this warning. You can use this instead of casting yourself: http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#asSubclass(java.lang.Class)

提交回复
热议问题