Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which line is an inappropriate use of an assertion?

public class Sample26
{
    static
    {
        assert load() : "load failed";
    }

    static boolean load() { return false; }

    public static void main(String[] args)
    {
        System.out.println("started");
    }
}

  • A.No line here is in error.
  • B.assert load() : "load failed";
  • C.static boolean load() { return false; }
  • D.System.out.println("started");

Answer: B

Initialisation work must not depend on an assertion. With -ea the failing assertion escapes static initialisation as ExceptionInInitializerError and main never runs; without it load() is never called at all. Either way the class's readiness is decided by a debugging flag.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.