Implementing reflection with factory pattern












0















I'm running this code but I'm getting this error message where I couldn't figure it out. It is asked to design any Java code using the factory pattern with the help of reflection. Below I added the error message that appears when I run the code and btw my file name and the class name is TestReflectionFactoryDesign.



Error message:



Exception in thread "main" java.lang.ClassNotFoundException: com.test.TestReflectionFactoryDesign.Student
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at PersonFactory.getPersonWithFullQualifiedClassName(TestReflectionFactoryDesign.java:58)
at TestReflectionFactoryDesign.main(TestReflectionFactoryDesign.java:6)


Code:



public class TestReflectionFactoryDesign {
public static void main(String args) throws Exception {
Person student = PersonFactory.getPersonWithFullQualifiedClassName("com.test.TestReflectionFactoryDesign.Student");
student.say();
Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
teacher.say();
Person student2 = PersonFactory.getPersonWithName("student");
student2.say();
}
}

class Student implements Person {
@Override
public void say() {
System.out.println("I am a student");
}
}

class Teacher implements Person {
@Override
public void say() {
System.out.println("I am a teacher");
}
}

interface Person {
void say();
}

class PersonFactory {
// reflection, by full qualified class name
public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
Class<?> personClass = Class.forName(personType);
return getPersonWithClass(personClass);
}

// reflection, by passing class object
public static Person getPersonWithClass(Class personClass) throws Exception {
return (Person) personClass.newInstance();
}

// no reflection, the ordinary way
public static Person getPersonWithName(String personType) {
if (personType.equalsIgnoreCase("STUDENT")) {
return new Student();
} else if (personType.equalsIgnoreCase("TEACHER")) {
return new Teacher();
}
return null;
}
}









share|improve this question

























  • com.test.TestReflectionFactoryDesign.Student says that Student is defined inside TestReflectionFactoryDesign, which it is not.

    – khelwood
    Dec 31 '18 at 9:38











  • package com.test.TestReflectionFactoryDesign do you have this line in your java file?

    – ZhaoGang
    Dec 31 '18 at 9:39











  • what package name of your class?

    – m fauzan abdi
    Dec 31 '18 at 9:40











  • No i dont have it

    – Mohimen Elosta
    Dec 31 '18 at 9:40











  • You need to pass the proper package where the student class resides.

    – KiraAG
    Dec 31 '18 at 9:42
















0















I'm running this code but I'm getting this error message where I couldn't figure it out. It is asked to design any Java code using the factory pattern with the help of reflection. Below I added the error message that appears when I run the code and btw my file name and the class name is TestReflectionFactoryDesign.



Error message:



Exception in thread "main" java.lang.ClassNotFoundException: com.test.TestReflectionFactoryDesign.Student
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at PersonFactory.getPersonWithFullQualifiedClassName(TestReflectionFactoryDesign.java:58)
at TestReflectionFactoryDesign.main(TestReflectionFactoryDesign.java:6)


Code:



public class TestReflectionFactoryDesign {
public static void main(String args) throws Exception {
Person student = PersonFactory.getPersonWithFullQualifiedClassName("com.test.TestReflectionFactoryDesign.Student");
student.say();
Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
teacher.say();
Person student2 = PersonFactory.getPersonWithName("student");
student2.say();
}
}

class Student implements Person {
@Override
public void say() {
System.out.println("I am a student");
}
}

class Teacher implements Person {
@Override
public void say() {
System.out.println("I am a teacher");
}
}

interface Person {
void say();
}

class PersonFactory {
// reflection, by full qualified class name
public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
Class<?> personClass = Class.forName(personType);
return getPersonWithClass(personClass);
}

// reflection, by passing class object
public static Person getPersonWithClass(Class personClass) throws Exception {
return (Person) personClass.newInstance();
}

// no reflection, the ordinary way
public static Person getPersonWithName(String personType) {
if (personType.equalsIgnoreCase("STUDENT")) {
return new Student();
} else if (personType.equalsIgnoreCase("TEACHER")) {
return new Teacher();
}
return null;
}
}









share|improve this question

























  • com.test.TestReflectionFactoryDesign.Student says that Student is defined inside TestReflectionFactoryDesign, which it is not.

    – khelwood
    Dec 31 '18 at 9:38











  • package com.test.TestReflectionFactoryDesign do you have this line in your java file?

    – ZhaoGang
    Dec 31 '18 at 9:39











  • what package name of your class?

    – m fauzan abdi
    Dec 31 '18 at 9:40











  • No i dont have it

    – Mohimen Elosta
    Dec 31 '18 at 9:40











  • You need to pass the proper package where the student class resides.

    – KiraAG
    Dec 31 '18 at 9:42














0












0








0








I'm running this code but I'm getting this error message where I couldn't figure it out. It is asked to design any Java code using the factory pattern with the help of reflection. Below I added the error message that appears when I run the code and btw my file name and the class name is TestReflectionFactoryDesign.



Error message:



Exception in thread "main" java.lang.ClassNotFoundException: com.test.TestReflectionFactoryDesign.Student
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at PersonFactory.getPersonWithFullQualifiedClassName(TestReflectionFactoryDesign.java:58)
at TestReflectionFactoryDesign.main(TestReflectionFactoryDesign.java:6)


Code:



public class TestReflectionFactoryDesign {
public static void main(String args) throws Exception {
Person student = PersonFactory.getPersonWithFullQualifiedClassName("com.test.TestReflectionFactoryDesign.Student");
student.say();
Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
teacher.say();
Person student2 = PersonFactory.getPersonWithName("student");
student2.say();
}
}

class Student implements Person {
@Override
public void say() {
System.out.println("I am a student");
}
}

class Teacher implements Person {
@Override
public void say() {
System.out.println("I am a teacher");
}
}

interface Person {
void say();
}

class PersonFactory {
// reflection, by full qualified class name
public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
Class<?> personClass = Class.forName(personType);
return getPersonWithClass(personClass);
}

// reflection, by passing class object
public static Person getPersonWithClass(Class personClass) throws Exception {
return (Person) personClass.newInstance();
}

// no reflection, the ordinary way
public static Person getPersonWithName(String personType) {
if (personType.equalsIgnoreCase("STUDENT")) {
return new Student();
} else if (personType.equalsIgnoreCase("TEACHER")) {
return new Teacher();
}
return null;
}
}









share|improve this question
















I'm running this code but I'm getting this error message where I couldn't figure it out. It is asked to design any Java code using the factory pattern with the help of reflection. Below I added the error message that appears when I run the code and btw my file name and the class name is TestReflectionFactoryDesign.



Error message:



Exception in thread "main" java.lang.ClassNotFoundException: com.test.TestReflectionFactoryDesign.Student
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at PersonFactory.getPersonWithFullQualifiedClassName(TestReflectionFactoryDesign.java:58)
at TestReflectionFactoryDesign.main(TestReflectionFactoryDesign.java:6)


Code:



public class TestReflectionFactoryDesign {
public static void main(String args) throws Exception {
Person student = PersonFactory.getPersonWithFullQualifiedClassName("com.test.TestReflectionFactoryDesign.Student");
student.say();
Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
teacher.say();
Person student2 = PersonFactory.getPersonWithName("student");
student2.say();
}
}

class Student implements Person {
@Override
public void say() {
System.out.println("I am a student");
}
}

class Teacher implements Person {
@Override
public void say() {
System.out.println("I am a teacher");
}
}

interface Person {
void say();
}

class PersonFactory {
// reflection, by full qualified class name
public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
Class<?> personClass = Class.forName(personType);
return getPersonWithClass(personClass);
}

// reflection, by passing class object
public static Person getPersonWithClass(Class personClass) throws Exception {
return (Person) personClass.newInstance();
}

// no reflection, the ordinary way
public static Person getPersonWithName(String personType) {
if (personType.equalsIgnoreCase("STUDENT")) {
return new Student();
} else if (personType.equalsIgnoreCase("TEACHER")) {
return new Teacher();
}
return null;
}
}






java reflection abstract-factory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 19:07









halfer

14.5k758111




14.5k758111










asked Dec 31 '18 at 9:33









Mohimen ElostaMohimen Elosta

11




11













  • com.test.TestReflectionFactoryDesign.Student says that Student is defined inside TestReflectionFactoryDesign, which it is not.

    – khelwood
    Dec 31 '18 at 9:38











  • package com.test.TestReflectionFactoryDesign do you have this line in your java file?

    – ZhaoGang
    Dec 31 '18 at 9:39











  • what package name of your class?

    – m fauzan abdi
    Dec 31 '18 at 9:40











  • No i dont have it

    – Mohimen Elosta
    Dec 31 '18 at 9:40











  • You need to pass the proper package where the student class resides.

    – KiraAG
    Dec 31 '18 at 9:42



















  • com.test.TestReflectionFactoryDesign.Student says that Student is defined inside TestReflectionFactoryDesign, which it is not.

    – khelwood
    Dec 31 '18 at 9:38











  • package com.test.TestReflectionFactoryDesign do you have this line in your java file?

    – ZhaoGang
    Dec 31 '18 at 9:39











  • what package name of your class?

    – m fauzan abdi
    Dec 31 '18 at 9:40











  • No i dont have it

    – Mohimen Elosta
    Dec 31 '18 at 9:40











  • You need to pass the proper package where the student class resides.

    – KiraAG
    Dec 31 '18 at 9:42

















com.test.TestReflectionFactoryDesign.Student says that Student is defined inside TestReflectionFactoryDesign, which it is not.

– khelwood
Dec 31 '18 at 9:38





com.test.TestReflectionFactoryDesign.Student says that Student is defined inside TestReflectionFactoryDesign, which it is not.

– khelwood
Dec 31 '18 at 9:38













package com.test.TestReflectionFactoryDesign do you have this line in your java file?

– ZhaoGang
Dec 31 '18 at 9:39





package com.test.TestReflectionFactoryDesign do you have this line in your java file?

– ZhaoGang
Dec 31 '18 at 9:39













what package name of your class?

– m fauzan abdi
Dec 31 '18 at 9:40





what package name of your class?

– m fauzan abdi
Dec 31 '18 at 9:40













No i dont have it

– Mohimen Elosta
Dec 31 '18 at 9:40





No i dont have it

– Mohimen Elosta
Dec 31 '18 at 9:40













You need to pass the proper package where the student class resides.

– KiraAG
Dec 31 '18 at 9:42





You need to pass the proper package where the student class resides.

– KiraAG
Dec 31 '18 at 9:42












2 Answers
2






active

oldest

votes


















0














As you (most probably) have not declared your class under a package, using the package xxx.yyy.zzz in the first line of your class, Java considers it to be part of the unnamed (default) package, as very well already explained here



You can fix that either by setting a package to your class file (most recommended), or changing the fully qualified class name to remove the invalid package (just keep Student).






share|improve this answer































    0














    If you only what to learn the factory pattern with the reflection, one simple solution is that just copy below code into a TestReflectionFactoryDesign.java file with your favorite text editor, and go to the path you saved it, and run javac TestReflectionFactoryDesign.java and then java TestReflectionFactoryDesign to see the result.



    public class TestReflectionFactoryDesign {
    public static void main(String args) throws Exception {
    Person student = PersonFactory.getPersonWithFullQualifiedClassName("Student");
    student.say();
    Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
    teacher.say();
    Person student2 = PersonFactory.getPersonWithName("student");
    student2.say();
    }
    }

    class Student implements Person {
    @Override
    public void say() {
    System.out.println("I am a student");
    }
    }

    class Teacher implements Person {
    @Override
    public void say() {
    System.out.println("I am a teacher");
    }
    }

    interface Person {
    void say();
    }

    class PersonFactory {
    // reflection, by full qualified class name
    public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
    Class<?> personClass = Class.forName(personType);
    return getPersonWithClass(personClass);
    }

    // reflection, by passing class object
    public static Person getPersonWithClass(Class personClass) throws Exception {
    return (Person) personClass.newInstance();
    }

    // no reflection, the ordinary way
    public static Person getPersonWithName(String personType) {
    if (personType.equalsIgnoreCase("STUDENT")) {
    return new Student();
    } else if (personType.equalsIgnoreCase("TEACHER")) {
    return new Teacher();
    }
    return null;
    }
    }


    If you are in an IDE, and don't want to change your code, you should put package com.test.TestReflectionFactoryDesign in the very beginning of your code, which should be the path where you created your java file.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985828%2fimplementing-reflection-with-factory-pattern%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      As you (most probably) have not declared your class under a package, using the package xxx.yyy.zzz in the first line of your class, Java considers it to be part of the unnamed (default) package, as very well already explained here



      You can fix that either by setting a package to your class file (most recommended), or changing the fully qualified class name to remove the invalid package (just keep Student).






      share|improve this answer




























        0














        As you (most probably) have not declared your class under a package, using the package xxx.yyy.zzz in the first line of your class, Java considers it to be part of the unnamed (default) package, as very well already explained here



        You can fix that either by setting a package to your class file (most recommended), or changing the fully qualified class name to remove the invalid package (just keep Student).






        share|improve this answer


























          0












          0








          0







          As you (most probably) have not declared your class under a package, using the package xxx.yyy.zzz in the first line of your class, Java considers it to be part of the unnamed (default) package, as very well already explained here



          You can fix that either by setting a package to your class file (most recommended), or changing the fully qualified class name to remove the invalid package (just keep Student).






          share|improve this answer













          As you (most probably) have not declared your class under a package, using the package xxx.yyy.zzz in the first line of your class, Java considers it to be part of the unnamed (default) package, as very well already explained here



          You can fix that either by setting a package to your class file (most recommended), or changing the fully qualified class name to remove the invalid package (just keep Student).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 9:45









          JChristJChrist

          1,3111521




          1,3111521

























              0














              If you only what to learn the factory pattern with the reflection, one simple solution is that just copy below code into a TestReflectionFactoryDesign.java file with your favorite text editor, and go to the path you saved it, and run javac TestReflectionFactoryDesign.java and then java TestReflectionFactoryDesign to see the result.



              public class TestReflectionFactoryDesign {
              public static void main(String args) throws Exception {
              Person student = PersonFactory.getPersonWithFullQualifiedClassName("Student");
              student.say();
              Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
              teacher.say();
              Person student2 = PersonFactory.getPersonWithName("student");
              student2.say();
              }
              }

              class Student implements Person {
              @Override
              public void say() {
              System.out.println("I am a student");
              }
              }

              class Teacher implements Person {
              @Override
              public void say() {
              System.out.println("I am a teacher");
              }
              }

              interface Person {
              void say();
              }

              class PersonFactory {
              // reflection, by full qualified class name
              public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
              Class<?> personClass = Class.forName(personType);
              return getPersonWithClass(personClass);
              }

              // reflection, by passing class object
              public static Person getPersonWithClass(Class personClass) throws Exception {
              return (Person) personClass.newInstance();
              }

              // no reflection, the ordinary way
              public static Person getPersonWithName(String personType) {
              if (personType.equalsIgnoreCase("STUDENT")) {
              return new Student();
              } else if (personType.equalsIgnoreCase("TEACHER")) {
              return new Teacher();
              }
              return null;
              }
              }


              If you are in an IDE, and don't want to change your code, you should put package com.test.TestReflectionFactoryDesign in the very beginning of your code, which should be the path where you created your java file.






              share|improve this answer




























                0














                If you only what to learn the factory pattern with the reflection, one simple solution is that just copy below code into a TestReflectionFactoryDesign.java file with your favorite text editor, and go to the path you saved it, and run javac TestReflectionFactoryDesign.java and then java TestReflectionFactoryDesign to see the result.



                public class TestReflectionFactoryDesign {
                public static void main(String args) throws Exception {
                Person student = PersonFactory.getPersonWithFullQualifiedClassName("Student");
                student.say();
                Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
                teacher.say();
                Person student2 = PersonFactory.getPersonWithName("student");
                student2.say();
                }
                }

                class Student implements Person {
                @Override
                public void say() {
                System.out.println("I am a student");
                }
                }

                class Teacher implements Person {
                @Override
                public void say() {
                System.out.println("I am a teacher");
                }
                }

                interface Person {
                void say();
                }

                class PersonFactory {
                // reflection, by full qualified class name
                public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
                Class<?> personClass = Class.forName(personType);
                return getPersonWithClass(personClass);
                }

                // reflection, by passing class object
                public static Person getPersonWithClass(Class personClass) throws Exception {
                return (Person) personClass.newInstance();
                }

                // no reflection, the ordinary way
                public static Person getPersonWithName(String personType) {
                if (personType.equalsIgnoreCase("STUDENT")) {
                return new Student();
                } else if (personType.equalsIgnoreCase("TEACHER")) {
                return new Teacher();
                }
                return null;
                }
                }


                If you are in an IDE, and don't want to change your code, you should put package com.test.TestReflectionFactoryDesign in the very beginning of your code, which should be the path where you created your java file.






                share|improve this answer


























                  0












                  0








                  0







                  If you only what to learn the factory pattern with the reflection, one simple solution is that just copy below code into a TestReflectionFactoryDesign.java file with your favorite text editor, and go to the path you saved it, and run javac TestReflectionFactoryDesign.java and then java TestReflectionFactoryDesign to see the result.



                  public class TestReflectionFactoryDesign {
                  public static void main(String args) throws Exception {
                  Person student = PersonFactory.getPersonWithFullQualifiedClassName("Student");
                  student.say();
                  Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
                  teacher.say();
                  Person student2 = PersonFactory.getPersonWithName("student");
                  student2.say();
                  }
                  }

                  class Student implements Person {
                  @Override
                  public void say() {
                  System.out.println("I am a student");
                  }
                  }

                  class Teacher implements Person {
                  @Override
                  public void say() {
                  System.out.println("I am a teacher");
                  }
                  }

                  interface Person {
                  void say();
                  }

                  class PersonFactory {
                  // reflection, by full qualified class name
                  public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
                  Class<?> personClass = Class.forName(personType);
                  return getPersonWithClass(personClass);
                  }

                  // reflection, by passing class object
                  public static Person getPersonWithClass(Class personClass) throws Exception {
                  return (Person) personClass.newInstance();
                  }

                  // no reflection, the ordinary way
                  public static Person getPersonWithName(String personType) {
                  if (personType.equalsIgnoreCase("STUDENT")) {
                  return new Student();
                  } else if (personType.equalsIgnoreCase("TEACHER")) {
                  return new Teacher();
                  }
                  return null;
                  }
                  }


                  If you are in an IDE, and don't want to change your code, you should put package com.test.TestReflectionFactoryDesign in the very beginning of your code, which should be the path where you created your java file.






                  share|improve this answer













                  If you only what to learn the factory pattern with the reflection, one simple solution is that just copy below code into a TestReflectionFactoryDesign.java file with your favorite text editor, and go to the path you saved it, and run javac TestReflectionFactoryDesign.java and then java TestReflectionFactoryDesign to see the result.



                  public class TestReflectionFactoryDesign {
                  public static void main(String args) throws Exception {
                  Person student = PersonFactory.getPersonWithFullQualifiedClassName("Student");
                  student.say();
                  Person teacher = PersonFactory.getPersonWithClass(Teacher.class);
                  teacher.say();
                  Person student2 = PersonFactory.getPersonWithName("student");
                  student2.say();
                  }
                  }

                  class Student implements Person {
                  @Override
                  public void say() {
                  System.out.println("I am a student");
                  }
                  }

                  class Teacher implements Person {
                  @Override
                  public void say() {
                  System.out.println("I am a teacher");
                  }
                  }

                  interface Person {
                  void say();
                  }

                  class PersonFactory {
                  // reflection, by full qualified class name
                  public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
                  Class<?> personClass = Class.forName(personType);
                  return getPersonWithClass(personClass);
                  }

                  // reflection, by passing class object
                  public static Person getPersonWithClass(Class personClass) throws Exception {
                  return (Person) personClass.newInstance();
                  }

                  // no reflection, the ordinary way
                  public static Person getPersonWithName(String personType) {
                  if (personType.equalsIgnoreCase("STUDENT")) {
                  return new Student();
                  } else if (personType.equalsIgnoreCase("TEACHER")) {
                  return new Teacher();
                  }
                  return null;
                  }
                  }


                  If you are in an IDE, and don't want to change your code, you should put package com.test.TestReflectionFactoryDesign in the very beginning of your code, which should be the path where you created your java file.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 9:48









                  ZhaoGangZhaoGang

                  1,9641116




                  1,9641116






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985828%2fimplementing-reflection-with-factory-pattern%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'