The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jul 11, 2001
BTW, in some more object oriented languages than Java, Smalltalk for example, classes actually are objects, too.
Joined: Mar 17, 2004
ok, suppose i have not instatiated any object of type MyClass yet, does the Class instance for MyClass already exist? no, i think...so is it instantiaded the first time you create a MyClass obj? Is there just ONE instance of Class relating to MyClass even if there are 200 MyClass objects?
Originally posted by Lukas Alamar: ok, suppose i have not instatiated any object of type MyClass yet, does the Class instance for MyClass already exist? no, i think...so is it instantiaded the first time you create a MyClass obj?
The Class object for MyClass is created when MyClass is loaded. When this happens, exactly, depends on how MyClass is used. Basically, MyClass is loaded as soon as any code refers to MyClass. If class X has member variables of type MyClass, then MyClass is loaded as soon as X is, even if those variables aren't initialized with new objects. On the other hand, if MyClass is only mentioned inside of a method y() of class X, then MyClass won't be loaded until y() is called.
But as far as MyClass.class goes: you will never find that ".class" is null. Java always loads classes just before they're used.
Is there just ONE instance of Class relating to MyClass even if there are 200 MyClass objects?
Yes, just one, whether there are zero or 200 MyClass objects.
Unless, of course, MyClass is loaded by more than one ClassLoader. But that's an advanced topic, and you won't run into it accidentally.