Reputation: 131
Recently while developing a swing application somebody told me to use (ComponentClassName) Beans.instantiate(MyClass.class.getClassLoader(), ComponentClassName.class.getName())
to create component object instead of using "new". I would like to know the right approach.
Upvotes: 0
Views: 104
Reputation: 2371
I think the right approach will be to use the new keyword, after all, that's it's role. The way you are using Beans seems a bit forced since you know what type of class you wish to instantiate. Second, since this is a swing application, most probably you are using default swing classes (maybe extending some of them). Another problem with this approach is that you must have a constructor without args in your class. Class.forName() can be used for the same thing as the code above, but if you don't want to dynamically create a class at runtime, stick with the new keyword.
Upvotes: 1
Reputation: 1390
You can use Beans.instantiate() if the type you want to instantiate is chosen dynamically. Generally the new keyword works just fine, and is what I always use in Swing applications.
Upvotes: 1