Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gireesh Giri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Language Specification and Java Virtual Machine Specification. They are probably not at all easy to read, however.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic