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();
3
  • You can then use Reflection to create an instance . Commented Mar 24, 2015 at 2:10
  • 1
    Object obj1 = Class.forName("test.company"+companyId + ".Products").newInstance(); Commented Mar 24, 2015 at 2: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(); Commented Mar 24, 2015 at 2:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.