• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

class loading

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain me...
What does class loading mean ?
what is the role of meta class 'Class' in this ?
 
Trailboss
Posts: 23453
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When your program needs a class that is not currently in memory, java goes to get the class, load it into memory and activate all of its static initializers. It also creates a Class object (from the class Class) to represent the class that was just loaded. In the Class object is stuff like the name of the class, the names of public methods and attributes, etc. This is used for reflection, serialization etc.
I have to confess that my experienc with the class Class is very limited. Hopefully someone else can stand up and fill in information from a more experienced perspective.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are Static initializers executed upon some condition ?
Does the initialization of class is done even if u don't use any of the instance variables ?
What is ClassLoader class for ? Does each class has a seperate ClassLoader or each class instance object ?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Are Static initializers executed upon some condition ?"
Nope! Unless you consider the loading of the class a "condition". They are always executed. Even before a constructor is. If a constructor is going to be called in the first place. (Which it may not.)
"Does the initialization of class is done even if u don't use any of the instance variables ?"
Sure! Witness the <CODE>java.lang.Math</CODE> class (for example). <CODE>Math.PI</CODE>, to name only one, has to be initialized before it's used.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Math.PI is a constant ( final variable ) is n't it !! So, it must be stored in the constant pool of a .class file. So, if one wants to use this PI constant, that value can be obtained directly from the class file. Am I right ?
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
The class loader will load a classfile when some thread asks JVM
to get any access to any class field (static methods, variables
or constructor). Class loader will make no distinction between
all of that actions.
There can also be just static classes, that have no way of being
instantiated. And it's due to class loader to initialiaze every
static field.

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know when the static(class) variables be garbage collected? When no instance of the class exist? Does anyone see anything about this in JAVA/JVM spec?
Thanks.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, variables are NEVER garbage collected. Only objects on the heap are garbage collected. Variables are created in a stack and are destroyed when the method goes out of scope, when the object whose field that they are is gone, or in the case of static variables, when the class is unloaded.
There does not need to be an instance of the class in order for the static variables to be created. Consider the Math class. There are NEVER any instances of that around, but the static variables pi and e are always available to be used. Just mentioning one of the static variables of a class or one of it's methods is enough to get it loaded.


2.17.8 Unloading of Classes and Interfaces
A class or interface may be unloaded if and only if its class loader is unreachable. The bootstrap class loader is always reachable; as a result, system classes may never be unloaded.


 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like a loaded class will stay in the memory until the JVM exits out. I have following questions
(1) In an Application serevr, like Weblogic or Websphere, environment, will loaded classes stay there until server is shut down?
(2)Are only those classes being called(instantiated or executed)from the main thread and down the pipe will be loaded? or these above plus all the JAVA foundation classes? or it is up to the implementation of JVMs as far as what classes need to be loaded initially when JVM starts?
(3)Anyway to force unload unneeded classes by usinf garbage collection or any other means?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Yes they will stay loaded. The reasoning is that a class may have:
- Static variables (whose state would be lost).
- Static initializers (which may have side effects).
- Native methods (which may retain static state).
If you allow a class to unload, and therefore possible RELOAD, the side effects from the above would be unacceptable.
2)Only those classes that have been mentioned in some manner are loaded. The foundation classes are loaded as needed.
3)If you are using a classloader other than the bootstrap loader (the normal one), and you remove all references to that classloader, then it will get garbage collected. At that time any classes that it had caused to be loaded will be unloaded. Any class that is loaded by the bootstrap classloader will NEVER be unloaded.
And to top it off a little quote from the JLS:


Strictly speaking, it is not essential that the issue of class unloading be discussed by this specification, as class unloading is merely an optimization. However, the issue is very subtle, and so it is mentioned here by way of clarification.


[This message has been edited by Cindy Glass (edited June 15, 2001).]
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic