Alfred James
Alfred James

Reputation: 1019

"Class" object can't be created

I am making a Class object in order to start a new activity. But the problem is when I type

Class ourclass = new Class.__

It doesn't recognise the Class and I can't use the methods of the super class "Class". It says class is a raw type. What type should I assign it to, because I want to use "forName()" method in which I want to pass a class name. Here is the code

    protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    try
    {
        Class ourclass =Class.__ // The suggestions for static functions doesn't popup, and I get errors
        //Intent myintent = new Intent();
    }
    catch(ClassNotFoundException e)
    {
        e.printStackTrace();
    }

}
}

Regards.

Upvotes: 0

Views: 121

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500795

What type should I assign it to, because I want to use "forName()" method in which I want to pass a class name.

Class.forName is a static method - you don't call it on an instance:

Class<?> ourClass = Class.forName("foo.bar.Baz");

(Whether this is actually appropriate or not when starting an activity is a different matter, but this answer just addresses the Class.forName part...)

Upvotes: 3

Louis Wasserman
Louis Wasserman

Reputation: 198123

If you want to use Class.forName, just call Class.forName(name), don't use a (nonexistent) constructor. forName is a static method, so you don't need a Class to invoke it on.

Upvotes: 0

Related Questions