4

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?

2
  • Guys..you all are correct..Its working. 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 Commented Apr 7, 2014 at 6:43
  • I cannot change the "IBugReportFactory" to "Class" because my another methods are using this instance and i need instance of this class in that method Commented Apr 7, 2014 at 6:59

8 Answers 8

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Or simply BugReportFactoryAndroid.class
@EyalSchneider : Was on the way of adding it.
I cannot change the "IBugReportFactory" to "Class" because my another methods are using this instance and i need instance of this class in that method
4
BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

Comments

1

You have two options:

BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

Or:

BugReportFactory.addFactoryImpl((new BugReportFactoryAndroid()).getClass());

I personally prefer the first one.

Comments

1

(ObjectInstance).getClass() or (Class Name).class

Comments

1

The Class is some kind of blueprint for an object. When you call new X(), you create a new object of the class X, which means, you build a new class following the blueprint of X.

In your case, the blueprint is wanted, so you need to put BugReportFactoryAndroid.class

Read more at http://www.programmerinterview.com/index.php/java-questions/difference-between-object-and-class/

Comments

1

If you want to pass the class in your method, the calling code will be:

BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

If you actually wanted to pass an instance of this class, then your method signature should change to:

public static void addFactoryImpl(Object impl) { }

OR

public static <T> void addFactoryImpl(T impl) { }

Comments

1

Actually I don't think you want to pass a class of type Class as argument. This is a special class used for reflection purposes.

In the case of the Abstract Factory design pattern, typically you have a common interface describing the factory API, and multiple implementations of this interface:

public interface BugReportFactory {
  public A createA();
  public B createB();
}

public class BugReportFactoryAndroid implements BugReportFactory {..}

public class BugReportFactoryIOS implements BugReportFactory {..}

And the class that invokes the factory methods may look like:

public class Foo {
  private List<BugReportFactory> factories = new ArrayList<BugReportFactory>(); 

  public void addFactoryImpl(BugReportFactory factory) { 
    factories.add(factory);
  }

  public void createAll() {
    for (BugReportFactory f : factories) {
      A a = f.createA(); 
      B b = f.createB();
      ... 
    }
  }
}

Comments

1

Finally I found solution both of my Problem: It is as follow:

private static IBugReportFactory INSTANCE = null;
public static void addFactoryImpl(Class<?> impl) {
    Object factoryImpl = null;
    try {
        factoryImpl = impl.newInstance();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    INSTANCE = (IBugReportFactory) factoryImpl;
}

If you want to call this method, it is like this way:

BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

Thanks Everyone for your support and help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.