Hi All,
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.
3. clone().
4. Class.newInstance().
5. Class.forName(String).
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.
Winston
Isn't it funny how there's always time and money enough to do it WRONG?
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 ?
Gireesh Giri
Greenhorn
Joined: Apr 23, 2010
Posts: 23
posted
0
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).
Stuart A. Burkett wrote: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 ?
Doh-h! You're right of course - except I think you meant #4 and #5.
@Gireesh: Forget items 4-6 from my list; they're for setting up instances of a Class, not an object.
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.
Stuart A. Burkett wrote: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.
Right again. My reading of the question was: what are the alternatives to new, when in fact it was: what are the alternatives to new {SomeClass}().
@Gireesh: Stuart's quite right. From my list 1,2, and 5-7 are possible alternatives, although 2 and 7 would be odd ways to do it.