aspose file tools*
The moose likes Beginning Java and the fly likes What it means of class loading and different ways to load a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What it means of class loading and different ways to load a class" Watch "What it means of class loading and different ways to load a class" New topic
Author

What it means of class loading and different ways to load a class

Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
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.

Thanks in advance.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7052
    
  16

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?
Artlicles by Winston can be found here
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Winston Gutkowski wrote:
3. clone().
4. Class.newInstance().

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
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
Bartender

Joined: Mar 17, 2011
Posts: 7052
    
  16

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Java Language Specification and Java Virtual Machine Specification. They are probably not at all easy to read, however.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7052
    
  16

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.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What it means of class loading and different ways to load a class
 
Similar Threads
failed on interview-question: local String-literal causes heap-allocation (?)
Loading an Array of Images
Singleton class + Lazy loading
Hibernate - load object's collection
Static and Dynamic class loading