This is referring to the class/interface loading by the class loader: What is active and passive usage of classes and interfaces? Could anyone please elaborate this concept a bit? Or at least refer me to some good material for the same.
Many thanks in advance.
Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
Hrishikesh Yeshwant Alshi wrote:This is related with SCJP.
What did you mean by active/passive usage? That's something I never heard before.
Since the underneath methodologies of ClassLoader are not bound to the scope of SCJP, I guess JiG is the best place to discuss this. If another moderator feels this is related to SCJP, this could be moved back to SCJP forum.
That page explains in a few places that classes or interfaces that are not actively used might not be initialized at all. There's this example:
And the text then says: "The class One is never initialized, because it not used actively and therefore is never linked to."
That means that when you run this program, the static initializer of class One will not be run, so you do not see the output "One". This is because there is no thread that needs class One, so the JVM will not load and initialize it. Classes are only loaded and initialized when they are actually needed.
There's no such thing as "passively" using a class.
following six situations qualify as active uses of a type
1. execution of 'new' instruction
2. Invocation of static method of a class.
3. Use or assignment of a static field declared by a class or interface, except for static final fields.
4. Invocation of certain reflective methods in Java API
5. Initialization of a subclass of a class
6. Designation of a class as the initial class(with main() method) when JVM starts
Example of passive use:
A use of a non-final field is an active use of only class that declares the field.
A static field declared in a class might be referred to via a subclass. This is a passive use of the subclass and active use of the super class.
This use will not trigger the initialization of the subclass.