been messing around with JFrames a bit, and I noticed something:
JFrame frame = new JFrame();
Works, from what I can tell, the exact same as:
JFrame frame = JFrame.class.newInstance();
Are there any differences between these two ways when just using the Default instance with no parameters?
Thanks,
Legend
The
newInstance()method injava.lang.Classconstructs an instance of the class using the no-argument contructor — so no, the twoJFrames would be identical.The
Class.newInstance()method becomes important when theClassobject is created using a class name at runtime — for example, a class name read from a file. Then you can sayNow you have an instance of a class whose name was not known when this code was compiled. If you know (by convention) that the named class implements some interface or extends some class, then you can cast the
Objectto that interface or class, and use it that way. This is how, for example, servlet containers load servlets as described in configuration files, or web browsers load applets named in HTML.