Java -verbose: the class indicates that the servlet is loaded, but static content is only initialized when the servlet is accessed

I think something is wrong here.

I have a simple one servletwith a static block that prints some kind of message. So, as far as I understand, the static block should be processed when the servlet class is first loaded into jvm.

I use the built-in jettyand start the berth server with the -verbose:classjvm option .

I see in the output that the class is servletloaded long before I first access this servlet and see the messages.

But, as far as I know, a static block should be initialized immediately after loading the class. Where am I wrong here?

+4
source share
2 answers

The Java language specification contains

The Java virtual machine dynamically loads , references, and initializes classes and interfaces. Downloading is the process of finding a binary file representing a class or interface type with a specific name and creating a class or interface from this binary representation. Binding is the process of taking a class or interface and merging it at run time of the Java virtual machine so that it can be executed. The initialization of a class or interface consists of executing the method of initializing a class or interface <clinit>(§2.9).

<clinit> - static .

java ,

-verbose:class

.

, -, , , Jetty , .

+2

... , Servlet , javax.servlet.Servlet?

<load-on-startup> web.xml, .


( , Servlet, , <load-on-startup> )

Java §8.7

, , (§12.4.2).

§12.4.1 ( 12.4.2) Java , T , :

  • T - T.
  • T - , T, .
  • , T.
  • , T, (§4.12.4).
  • T - (§7.6), assert (§14.10), T (§8.1.3).

, jetty ,

+2

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


All Articles