• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76817
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This tiny ad is guaranteed to be gluten free.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic