0

Using Java, I'm trying to access a package using an integer ID variable in order to get a packages named "test.company1" and "test.company2". I would then like to create objects of a class named "Products" that is within both packages. What would be the best way?

Package pkg = Package.getPackage("test.company"+companyId);
Class company1 = pkg.getClass("Products");
//to then be able to do something like 
//test.company1.Products products = new test.company1.Products();
//and test.company2.Products products = new test.company2.Products();
stevetronix
  • 1,231
  • 2
  • 16
  • 32
  • You can then use Reflection to create an instance . – PM 77-1 Mar 24 '15 at 02:10
  • 1
    Object obj1 = Class.forName("test.company"+companyId + ".Products").newInstance(); – Jason Mar 24 '15 at 02:16
  • How can I then turn the "obj" object into an object of the class that I used inside Class.forName(...) without explicitly typing it out? Object obj = Class.forName("test.company"+companyId+".Products").newInstance(); – stevetronix Mar 24 '15 at 02:35

0 Answers0