Is the Class object A created when the JVM loads class A, or when I call A.class?
Asked Answered
G

4

8

As I know, every class has a Class object. There is one case when I use synchronize, for example:

public class A {
    public static void main(String... args){
        synchronize(A.class){
            //do something...
        }
    }
}

This will lock A's Class object, right? When is this Class object created? Is it created when the JVM loads the A class or when I call A.class? I can't find detail in JLS, could someone please provide the link about it?

Gardener answered 28/9, 2015 at 9:41 Comment(0)
U
7

this will lock A's Class object, right?

yes.

my question is when this Class object is created? is it created at JVM load A class or when i call A.class?

When the ClassLoader loads it, it returns a Class object.

i can't find detail in JLS, could someone please provide the link about it, thanks.

I suggest reading the javadoc for the ClassLoader.loadClass()

Usage answered 28/9, 2015 at 9:48 Comment(11)
hi, Peter, also the same question, can it gc?Gardener
@Gardener a class can be GCed only if it's class loader is GC'ed. This means all instances of all classes for the classloader are no longer strongly referenced. The main/default ClassLoader is never GC'ed.Usage
thanks, A a = new A(), i can say a is an instance of A class, but A's Class object is who's instance, if it is belong to A, i don't think a = Class objectGardener
@PeterLawrey this is only in theory. In practice classloaders are never garbage collectedShelbashelbi
@Shelbashelbi i don't think so, in order to your words, class is never GC'ed, right? we can have custom classloader.Gardener
@Shelbashelbi in an Application servers such as OSGi server, class loaders are unloaded regulary. In plain Java programs you need to load an additional ClassLoader if you want to be able to unload that code later. It doesn't have to be a custom one.Usage
But OSGi servers have custom ClassLoaders:D In specific cases they have 1 classloader per jar file thus when classes from jar are not used anymore the classloader is unloaded. This is very specific case -> that is the reason that I wrote that in practice ClassLoaders are never garbage collected during your application runtime. They can be in OSGi during deployment, but it is very rare case during application runtime.Shelbashelbi
hi, could someone can answer my earlier sub question, whose instance is Class object(A) is?Gardener
@Gardener A.class is an instance of Class. Can you clarify your question?Usage
@PeterLawrey okay, is this Class object GC before or after class A GC'edGardener
@Gardener There is only one object to represent the class.Usage
C
2

It is created when the class is loaded by the JVM as the Javadocs states:

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

Clougher answered 28/9, 2015 at 9:46 Comment(2)
every class just have one instance of Class object, right? can it gc?Gardener
Yes one instance only. It can be GC'd in extreme cases.Clougher
T
2

A class is initialized by a ClassLoader when the class is first used, JLS 5.3:

5.3. Creation and Loading

Creation of a class or interface C denoted by the name N consists of the construction in the method area of the Java Virtual Machine (§2.5.4) of an implementation-specific internal representation of C. Class or interface creation is triggered by another class or interface D, which references C through its run-time constant pool. Class or interface creation may also be triggered by D invoking methods in certain Java SE platform class libraries (§2.12) such as reflection.

Tref answered 28/9, 2015 at 9:56 Comment(0)
C
0

Is it created when the JVM loads the A class

Yes.

or when I call A.class?

No. The JVM loads A when a class that uses it is loaded, apart from the special case of reflection, which doesn't apply here.

Calla answered 28/9, 2015 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.