Why is this class file created?

In Java, all classes are loaded dynamically in the JVM the first time you use a class.

Does this mean that if I have a class in the source file and I do not reference it, then its object is Classnot created (i.e. the .classfile is not created)?

In the code sample below, iam does not reference the test3class, but its class object is still created.

class test1 {
    static {
        System.out.println("static block of test1");
    }
}   
class test2{
    static {
        System.out.println("static block of test2");
    }
}
class test3 {}
class MyExample1 {
    public static void main(String ...strings ) {
    new test1();
    new test2();
    }
}

Why is the test3.classfile created ?

+3
source share
2 answers

The .class file was created at compile time. But it will be loaded from the .class file by first use (possibly).

Where should it be downloaded without a .class file?)

+6
source

test3.class ( ) test3.class test3, , .

class , .java ( ) ( class3.java, - , ) - , .

- , . URLClassLoader , " ", . () static. ( - , , .)

+1

Source: https://habr.com/ru/post/1796749/


All Articles