For static data members, memory will allocate and initialize with default values by JVM, while class loading time. I would like to know what all are the ways i can load a class other than(<class-name> <variable-name> = new <class-name>();) by creating the object.
Gireesh Chetty wrote:For static data members, memory will allocate and initialize with default values by JVM, while class loading time. I would like to know what all are the ways i can load a class other than(<class-name> <variable-name> = new <class-name>();) by creating the object.
Well, here are just a few:
1. A static factory method.
2. An instance factory method.
6. A custom class loader (advanced).
7. Reflective construction (don't try this at home).
However, I'm not sure that it answers your real question.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Surely these only create new instances of a class. The class loading would already have been done before either of these methods could be called, wouldn't it ?
Joined: Apr 23, 2010
Thanks Winston Gutkowski & Stuart A. Burkett,
Could any one please explain me what it means of "class loading".
I tried [5. Class.forName(String). ] this, it is allocated the memory for my static variables and static block executed successfully (If my understanding is correct ,this it will not create the object, only it load the class).
Winston Gutkowski wrote:Doh-h! You're right of course - except I think you meant #4 and #5.
No. #3 and #4.
#3 You need an instance of a class before you can clone it, so if you already have an instance, the class must have been loaded.
#4 newInstance is an instance method of the Class class. You need an instance of the Class class of the class you are trying to create an instance of in order to call the method and if you have this Class instance the class must have been loaded. (I think that makes sense - Class is definitely a bad name for a class)
#5 Class.forname is a static method of the Class class. It will cause the class specified in the parameter to be loaded, so can be called before that class is loaded.