for example I have following method in class BugReportFactory:
public static void addFactoryImpl(Class impl) { }
I want to call this method from another class by following ways:
BugReportFactory.addFactoryImpl(new BugReportFactoryAndroid());
It says that following Argument is not applicable for Class class.
Can anyone tell my mistake?
ONE MORE QUESTION:
private static IBugReportFactory INSTANCE = null;
public static void addFactoryImpl(Class impl) {
INSTANCE = (IBugReportFactory)impl;
}
But it shows errors specifying that you cannot cast class to object?
Try following, Object
class has a getClass()
method
BugReportFactory.addFactoryImpl(new BugReportFactoryAndroid().getClass());
Or
BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);
Will do the job.
ONE MORE QUESTION:
private static IBugReportFactory INSTANCE = null; public static void addFactoryImpl(Class impl) { INSTANCE = (IBugReportFactory)impl; }
But it shows errors specifying that you cannot cast
Class is different instance is different. Change your INSTANCE
Variable type as Class
.
private static Class INSTANCE = null;
public static void addFactoryImpl(Class impl) {
INSTANCE = impl;
}
Class is blue print of an instance. You can't assign an instance to Class reference. Both are two different things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With