• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName() question

 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The java documentation for the above method states that it "Returns the Class object associated with the class or interface with the given string name"
Does this actually create a Class object for the given class or just return a reference to a Class object which already exists and if the latter is the case when is this class object created?
Thanks
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not 100% certain about this, but I suspect it all depends. If the class has already been used, then probably the class loader creates the Class object when its first needed. If the class hasn't been used yet, then Class.forName() probably creates the class when you call it.
This is only a guess, though. Please someone correct me if I'm wrong.
Layne
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds reasonable to me.
Simon
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think, Class.forName() creates a Class object for that particular class which can be used to create an instance of the class. i.e
Class.forName().newInstance()
-------------
Nayan.
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nayanjyoti
Does this mean then that Class.forName returns a Class object but an instance of that class not need exist? Also as the method returns a Class its not clear if it created the Class object or just a reference to an existing one.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct an instance of the class need not exist. Once Class.forName() returns a Class object for the given class needs to exist, but it could have been created previous to the call to Class.forName(). As I said earlier, if an instance of Class exists for the class in question, I doubt another is created.
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys. One last slightly related question, am i right in saying that when an instance of a class is created, then an associated Class object is created or is a Class object created simply by importing a library or when creating a reference.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic