aspose file tools*
The moose likes Java in General and the fly likes Class.forName() question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class.forName() question" Watch "Class.forName() question" New topic
Author

Class.forName() question

k Oyedeji
Ranch Hand

Joined: Jul 07, 2002
Posts: 96
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Sounds reasonable to me.
Simon
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
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

Joined: Jul 07, 2002
Posts: 96
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Jul 07, 2002
Posts: 96
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
 
 
subject: Class.forName() question