Java Reference
In-Depth Information
Enabling Assertions
By default, assert statements are ignored by the JVM at runtime. To enable assertions, use
the -enableassertions fl ag on the command line:
java -enableassertions Rectangle
You can also use the shortcut -ea fl ag:
java -ea Rectangle
Using the -enableassertions fl ag without any arguments enables assertions in all
classes except system classes. You can also enable assertions for a specifi c class or package.
For example, the following command enables assertions only for classes in the com.sybex
.demos package and any subpackages:
java -ea:com.sybex.demos... my.programs.Main
If the classes are in the unnamed packaged, then simply use the three dots:
java -ea:... Rectangle
You can also enable assertions for a specifi c class:
java -ea:com.sybex.demos.TestColors my.programs.Main
You can disable assertions using the -disableassertions (or -da ) for a specifi c class
or package that was previously enabled. For example, the following command enables
assertions for the com.sybex.demos package, but disables assertions for the TestColors
class:
java -ea:com.sybex.demos... -da:com.sybex.demos.TestColors my.programs.Main
Enabling assertions is an important aspect of using them, because if
assertions are not enabled, assert statements are ignored at runtime.
Assertions were added to the Java language in the J2SE 1.4 release, as
was the new assert keyword. This was a fairly major addition to the Java
language, and you can expect at least one question on the syntax and
flow of control of an assertion, as well as at least one question on how to
enable assertions at runtime. Keep an eye out for a question that contains
an assert statement but that is not executed with assertions enabled; the
assert statement is ignored in that situation.
Search WWH ::




Custom Search