Home » Android » java – Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"?

java – Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am getting warning in my ListActivity. The warning I am getting is shown below

  • Class is a raw type. References to generic type Class<T> should be
    parameterized

It is not creating any problems, but I would like to know why I am getting this warning and how to suppress it. See line which written within asterisks.

public class Menu extends ListActivity {

    String classes[]={"Second","example1","example2","example3","example4"}; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String cheese=classes[position];
        try{
        **Class ourclass= Class.forName("com.app1."+cheese);**
        Intent ourintent= new Intent(Menu.this,ourclass);
        startActivity(ourintent);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
How to&Answers:

Class is generic, if you don’t care for the warning you have two choices use @SuppressWarnings("rawtypes") or my preference use the <?> (that is a wildcard capture) like this

Class<?> ourclass = Class.forName("com.app1."+cheese);

Answer:

You can use @SuppressWarnings("rawtypes","unchecked"). You can also make the code

Class<?> ourclass= Class.forName("com.app1."+cheese);

to get rid of the warning. Now, you don’t have to use @SuppressWarnings("rawtypes"). Compiler expects all the generic types to be parameterized

Answer:

To ignore the warning “Class is a raw type….” do the following inside eclipse*:

  1. Click Window-Preferences-Java-Compiler-Errors/Warnings
  2. Click “Generic Types”
  3. Choose “Ignore” for “Usage of a Raw Type”
  4. Click Apply
  5. Click OK
  6. Save and close your eclipse IDE

When you reopen eclipse, these specific warnings should no longer be listed.

*For this example solution I’m using Eclipse IDE for Java Developers – Version: Mars.2 Release (4.5.2)