• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instantiate object via class literal?

 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use newInstance() method. http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance()
Like jc = cl.newInstance();
 
Paul Clapham
Sheriff
Posts: 21116
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21116
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic