Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
Is it specified in the JVM/JLS that classes in unused code paths will never be loaded?

Given the following class using Java 8 Optional:

final class Main {
    public static void main(final String[] args) {
        System.out.println(Optional.of("test").get());
    }
}

If I compile the code with a Java 8 compiler targeting Java 7 bytecode:

javac -target 1.7 -source 1.7 Main.java

When I run with a Java 7 JVM, main throws a NoClassDefFoundError wrapping a ClassNotFoundException for java.util.Optional as expected.

However if I check for the availability of the Optional class (via reflection) prior to using it:

final class Main {
    public static void main(final String[] args) {
        if (isOptionalAvailable()) {
            System.out.println(Optional.of("test").get());
        } else {
            System.out.println("Optional not found.");
        }
    }

    private static boolean isOptionalAvailable() {
        try {
            Class.forName("java.util.Optional");
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
}

When I run with a Java 7 JVM, it does not throw any errors:

Optional not found.

What I’m trying to find out is if this behavior is required by the JVM or JLS specifications. It seems like the behavior is consistent across Oracle, IBM, and OpenJDK, but I couldn’t seem to find any requirements in the specs that classes used locally in methods must be lazily loaded.

I looked through "Chapter 5. Loading, Linking, and Initializing" of the JVM Spec and "15.12.4. Run-Time Evaluation of Method Invocation" in the JLS.

For my second example, could there be a JVM impl that eagerly loads Optional even though it only exists in an unused code path? Am I missing the section of the spec(s) that requires this behavior or is it just a common implementation detail?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There is no guaranty that the class doesn’t get loaded.

Consider JLS, §5.4:

This specification allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that all of the following properties are maintained:

…

For example, a Java Virtual Machine implementation may choose a "lazy" linkage strategy, where each symbolic reference in a class or interface (other than the symbolic references above) is resolved individually when it is used. Alternatively, an implementation may choose an "eager" linkage strategy, where all symbolic references are resolved at once when the class or interface is being verified.

Even the HotSpot JVM, which uses lazy class loading, may attempt to load the class earlier than expected, i.e. outside that unused code path, due to subtle aspects of the code, which may require the verifier to load a class, as discussed in When is a Java Class loaded?

In other words, even with this JVM implementation, small changes to the code may suddenly make it fail when the class is absent.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda