How to declare a package?

The package statement is used to declare packages. The word package is a keyword in Java. This statement is not technically needed to write a complete Java program. However, if it appears, it must be the first executable statement in the program. Only comments or white spaces are allowed before a package statement. There can be only one package declaration in a program. If no package declaration appears, the class will belong to the default package.

For example, the following source code file defines a class named ClassA in the package named package4:

package package4;

  • Share/Bookmark

What is an assertion?

An assertion is a statement that will be evauated during the execution of a program. It returns a boolean result. If the result is true, the code executes normally and no other action takes place. This confirms that the assumed statement is true. If the result is false, an AssertionError is thrown. The assertion condition is tested using the assert keyword. The assert keyword has the following two forms:

  • assert assertCondition;
    Here, assertCondition is an expression that evaluates to a boolean expression.
  • assert assertCondition : exp;
    Here, an expression exp is passed to the AssertionError in case an error is thrown at runtime.

An assertion can be enabled or disabled for a class or a package during runtime as follows:

  • Share/Bookmark