This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Check out the API documentation for the Class class (that's java.lang.Class). You'll see methods with names like newInstance() which you can use, and there are more complicated ways if you need to create an object using a constructor which takes parameters.
But if you're a beginner, you probably don't need to do this. Beginners who create designs using reflection (which is what you're asking about) usually do so because they haven't yet learned about the ordinary way to do whatever it is they are designing.
It’s There are other ways to get the Class<?> object. It depends on the class in question having an accessible no‑arguments constructor, and declares a list of checked Exceptions as long as your arm. You may need a class cast, too.
Also note, what you're talking about there is a Classobject. That's not the same as a class literal. A class literal is something like String.class, which is a way of getting the Class object for a class known to the compiler at compile time. But I don't think you'd ever use one to create a new object, because if you can use a class literal you can just use a new statement.
will probably throw an exception, unless you happen to have created a class named "JCheckBox" which isn't in a package. In particular it won't load the javax.swing.JCheckBox class because the name of that class is "javax.swing.JCheckBox" and not "JCheckBox".
And this line of code:
guarantees that you won't know what that exception was. You should never do this in real code, unless you're sure you don't care what exception was thrown and you're sure you don't need to know what it is when it is thrown. (There could be cases where you really don't give a rat's.) Instead use this code:
Joined: Feb 15, 2012
Thanks to you all for your help. Much appreciated.
I've now made myself a small program to play with, to try and understand this aspect of java a little better.
It's only short, so I've posted it below for the enjoyment of any future readers of this thread. I hope that's appropriate.
The user can choose from 3 radio buttons. Depending on which they choose, a variable c of type Class is set to JCheckBox.class or JButton.class and so on.
When the user clicks on the green panel, c is used to create a component of the chosen type.
Joined: Oct 13, 2005
Not bad, but I don’t like the listener. As far as I am concerned, addActionListener(this) is non‑object‑oriented programming.That will work best if ClassChangeListener is a private inner class (i.e. inside the GUI class, and not marked static), so it has access to the field c. You will doubtless have worked out how to fill in the bits of that class which I have omitted.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com