• Post Reply Bookmark Topic Watch Topic
  • New Topic

how does .newInstance() *really* work?  RSS feed

 
juliane gross
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I work on an assignment (not a cattle drive one), where I have
a Swing menu containing several JMenuItems.
Each of them is on click calling a class named
identically to the JMenuItem. The ActionListener loads
the class (each with an individual GUI).

the code so far works fine, but:
1. only if there is already a (old) version of my class,
the code will compile (otherwise I get a Class-not-found Error),
or if I compile my class seperately.
2. If there is a class found, the GUI builds up nicely,
but does NOT update any changes made to the code.
My Questions:
1. Why doesn't create myClass.new Instance() in the method "go" an updated version?
2. How else could I provide a "true" new Instance without calling each
class by name?
For example, if I de-comment this code, then the update works fine:
new JMenuItemClass1();
new JMenuItemClass2();
I was brooding over this for 2 afternoons already... :roll:
thanks for answering!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I don't really understand your questions, and I think this is because your understanding of what your code is supposed to do is flawed. I'll explain what the methods really do, and then you might want to ask a follow-up question.
Class.forName() returns a Class object representing the named class. It uses the "context ClassLoader" for the calling thread to find this Class object, which, all things being equal, is the system ClassLoader.
The ClassLoader, in turn, will first check to see if the class has already been loaded; if it has, it will return that Class object. Otherwise, it will look on disk for the .class file corresponding to that named class.
Note that the system ClassLoader can only load existing .class files. It cannot create any new classes out of thin air.
Any one ClassLoader can only load a given class once. The only way that a class can be "unloaded" is for the ClassLoader object that loaded it to itself be garbage-collected first. This obviously never happens with the system ClassLoader. To "reload" a class, you have to explicitly create a new ClassLoader and load the class through that new loader.

The newInstance() method of Class creates an instance of the given class cia the class's no-argument constructor. The following two lines:

are precisely equivalent to this one line

So, there you have it. If this doesn't clear things up, feel free to ask another question in this thread. You can also check out Ted Neward's wonderful paper on this topic.
 
juliane gross
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just posting a BIG THANK YOU for your elaborate answer, Ernest.
I come back here (if necessary) after I finished studying Ted Newards
Paper.
Thanks again,
Juliane
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!