0

I want to use Reflection to read the display function of each child class extends class Demo.

class Demo{
    public abstract void display();
}    

I want clazz only initialized once,I do not know how to check whether the clazz exists or not,please help me :D

public static void getDisplay(Class<? extends Demo> clazz) throws 
                   InstantiationException, IllegalAccessException {
        return clazz.newInstance().display();
}
Touya Akira
  • 301
  • 4
  • 14
  • 1
    Use singletons perhaps? This is probably an [XY Problem](http://xyproblem.info/). – shmosel Aug 09 '17 at 00:43
  • Hi Problem I can not test class not null or not because it is always valid.It is not null or empty – Touya Akira Aug 09 '17 at 00:48
  • 1
    I have no idea what that means. – shmosel Aug 09 '17 at 00:50
  • Hi @KiênĐịnh! use this link i hope it would help, https://stackoverflow.com/questions/27060999/java-checking-while-run-time-if-class-exists – Tehmina Aug 09 '17 at 01:31
  • Hi @Tehmina That article they test whether the class exists or not.But I want to check whether the class was initialized or not – Touya Akira Aug 09 '17 at 01:41
  • Got it. Try to replace line public static void getDisplay(Class extends Demo> clazz) to public static void getDisplay(Class> extends Demo clazz). The answer 3 of given link is the solution of your problem. https://stackoverflow.com/questions/28773806/java-call-method-after-class-is-initialized – Tehmina Aug 09 '17 at 04:26
  • @Tehmina I do not want to be lazy, even if you suggested I still do not succeed. If possible please help me with the command for this question – Touya Akira Aug 09 '17 at 15:18

2 Answers2

3

Try the below code according to your logic.

getSuperclass() method on a Class object returns the super class of the class. If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned.

    // First took class name by using Class.forName(String fullyClassifiedClassName).   
    Class<?> superClass = Class.forName("childclass").getSuperclass();
    System.out.println(superClass); // prints "childclass"
    System.out.println(Object.class.getSuperclass()); // prints "null"

or try this code, it is working fine on my system. I hope this would help.

public class Main {
    public static void main(String[] args) {
      clazz c = new clazz();
        c.display();
    }
}
   class Demo
   {
      // Apply superclass's logic here
   }

    // sub class
class clazz extends Demo {
   public void display()
   {
       Demo val1 = new Demo();
      clazz val2 = new clazz();
      Class cls;
      cls = val1.getClass();
      System.out.println("Super class name = " + cls.getName());
      /* returns the superclass of the class(superClass) represented
         by this object */
      cls = cls.getSuperclass();
      System.out.println("Super is inherited by another class " + cls.getName());
      cls = val2.getClass();
      System.out.println("child Class Name " + cls.getName());
      /* returns the superclass of the class(subClass) represented
         by this object */
      cls = cls.getSuperclass();
      System.out.println("Child class extended by " + cls.getName());
   }
}
Tehmina
  • 205
  • 2
  • 8
1

Why not turn getDisplay into a member method of a class?

That way you could save a list or map of clazz names and instances in the class where getDisplay lives. Check for membership in the list/map and return display() if it exists. If it doesn't exist, put it on the list/map, and return display().

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86