The constant pool contains String
instances, which are runtime artifacts. Clearly, you cannot create objects before you start the program they are used in. The data specifying which string constants will be created is prepared at compile time and is a part of the class file format.
However, note that the string constants are created at class loading time, and not on either class initialization time or their first use. This is a point which people often confuse.
In your example, the difference is not between compile time and runtime, but between creating the string once in the constant pool, and creating it every time a line of code is executed.
Also do note that the string pool has been a part of the regular heap for a long time in OpenJDK (even before it has become OpenJDK).