• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName()

 
Suresh Appagundi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes the difference between getting the instance of the Particular
class using
Class.forName() {what is dynamic loading? how it useful?}
and
new keyword?
thanks in advance
Suresh
 
Amit KumarS
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hiiiii....
as far as Class.forName() is concerned it is used to load a class at runtime. i.e. at the time of compilation we don't know which class should be loaded in memory but through programming we decide which class is to be loaded then we say
Class.forName("Foo");
this will load Foo into memory when this line is reached during runtime.
now if you want to make an instance of this at that time then you can call

now this will return an object of Class "Object" and you can typecast it to your class and then use it..
but new is used to create an object of the class when that class is already loaded in the memory. we use new for those classes which we have added in bytecode using import statement.
i hope i have answered your query.. if any doubt please revert back
Thanks
Amit
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of the examples of dynamic classloading are JDBC drivers, Servlet instance, EJB instances etc.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the intermediate format...
 
Jacquie Barker
author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class's byte code is loaded by the Java Virtual Machine at run time the first time an instance of that object is created using the "new" operator; but, if you want to load a class WITHOUT having to create an instance of it, Class.forName() does the trick!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic