Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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();
        }
    }
}
like image 745
hemkar Avatar asked Dec 08 '13 07:12

hemkar


3 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);
like image 122
Elliott Frisch Avatar answered Oct 14 '22 02:10

Elliott Frisch


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

like image 9
Keerthivasan Avatar answered Oct 14 '22 00:10

Keerthivasan


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)

like image 5
Mark Burleigh Avatar answered Oct 14 '22 02:10

Mark Burleigh