• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class.forName()

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some of the examples of dynamic classloading are JDBC drivers, Servlet instance, EJB instances etc.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the intermediate format...
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic