| Author |
Static Initializers and Static, Dynamic class loading
|
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
Hi all, I have read about static initializers online and understood that it's just another way of initializing variables(static variables only). I also read about static and dynamic class loading. static class loading means using the new keyword and creating an instance of the class and dynamic class loading means using Class.forName() method. Also, loading means creating the .class's object right? Am I correct? Please correct me if I am wrong. I do not understand why we need Class.forName() , the dynamic class loading when we can already load our class statically. Can someone please tell me why? Thanks.
|
Be Humble... Be Nice.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
We use Class.forName() and the like to load classes whose names we don't know when our code is compiled. For example, imagine you're writing a servlet container like Tomcat. You read a web.xml file and it tells you the class name of a servlet. How do you instantiate that servlet? By loading the class with Class.forName(), and using Class.newInstance() to create one. I hate the term "static class loading". I've heard it used before but it's an inappropriate and misleading term. All class loading is dynamic; it always happens at runtime.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
Um.. and the container does all this right? I mean instantiating the Servlet in the example you gave. Thanks. [ September 01, 2008: Message edited by: Arjun Reddy ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Originally posted by Arjun Reddy: Um.. and the container does all this right? I mean instantiating the Servlet in the example you gave.
Yes, so if you were writing a new servlet container, that's what you'd do. Likewise IDEs (i.e., plugin architectures), applet containers, and many other kinds of applications load classes by name.
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
|
Thanks Ernest understood it.
|
 |
 |
|
|
subject: Static Initializers and Static, Dynamic class loading
|
|
|