Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes instantiate object via class literal? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instantiate object via class literal?" Watch "instantiate object via class literal?" New topic
Author

instantiate object via class literal?

Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
Just got to page 738 k&b's book, where they introduce class literals.

Is it possible to use a class literal to instantiate an object?
I thought this should work:



But it doesn't.

I thought I'd need the cast to assure the compiler that the object being created is indeed something descendant from JComponent and therefore assignable to jc.


Regards
Richard
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Use newInstance() method. http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance()
Like jc = cl.newInstance();
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, there is.

But that isn't the way to do it.

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Also note, what you're talking about there is a Class object. 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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And by the way, this code:



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:



Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
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.

Regards

Richard


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
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.
 
Don't get me started about those stupid light bulbs.
 
subject: instantiate object via class literal?
 
Similar Threads
instanceof problem
ClasscastException in Collections.sort()
how to create an Array of JTextField !!!!
HELLP! i cant get 2 JPanels to display without them sitting on the other!
TableCellRenderer setBackground of selected cell not working