0

MyInterface.java

publc interface MyInterface{
   void print();
}

Abc.java

public Class Abc implements MyInterface{
          public void print(){
         System.out.print("Inside Abc");
     }
}

Xyz.java

public Class Xyz implements MyInterface{
          public void print(){
         System.out.print("Inside Xyz");
     }
}

Main.java

public Class Main{

public static void main(String arg[]){

  String classPath="Abc"; // this String will get assign @ runtime.
  Class<?> s = Class.forName(classPath);
}
}

Here inside main method classPath is "Abc", so i'm expecting Abc Instance. The classsPath string will be Abc or Xyz or any Class Name that implements MyInterface.So depending the classPath String i want the instance of that class. like if ClassPath is "Abc" then Abc Class instance, ClassPath is "Xyz" then Xyz Class instance. How can i achieve this dynamically.

4 Answers 4

1

You need to know what constructor to call.

Assuming all your classes have a no-argument constructor and you want that one:

MyInterface instance =  (MyInterface) s.newInstance();

If the constructor has a different signature, you need to supply that, for example with a single String parameter:

MyInterface instance = (MyInterface) s
              .getConstructor(String.class)
              .newInstance("foo");
Sign up to request clarification or add additional context in comments.

Comments

1

You can create an object dynamically at runtime using the name of the class, input as a simple string. This is done using a part of the Java language called reflection. Reflection allows old code to call new code, without needing to recompile. If a class has a no-argument constructor, then creating an object from its package-qualified class name (for example, "java.lang.Integer") is usually done using these methods:

Class.forName Class.newInstance If arguments need to be passed to the constructor, then these alternatives may be used instead:

Class.getConstructor Constructor.newInstance The most common use of reflection is to instantiate a class whose generic type is known at design-time, but whose specific implementation class is not. See the plugin topic for an example. Other uses of reflection are rather rare, and appear mostly in special-purpose programs.

Comments

0

I see a few typos in your post, so let's fix those first. public and class like

public interface MyInterface {
    void print();
}

public class Abc implements MyInterface {
    public void print() {
        System.out.print("Inside Abc");
    }
}

Then you use Class.newInstance() to create an Object, check that it's the expected type with instanceof and then cast like

public static void main(String[] args) {
    try {
        Class<?> cls = Class.forName("Abc");
        Object o = cls.newInstance();
        if (o instanceof MyInterface) {
            MyInterface m = (MyInterface) o;
            m.print();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Comments

0

newInstance Method of Class is used to create new Instance.

 public static void main(String arg[]){

  String classPath="Abc"; // this String will get assign @ runtime.
  Class s = Class.forName(classPath);
  Object object = s.newInstance();// to create new Instance
 }

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.