Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Initializers and Static, Dynamic class loading

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest understood it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic