June 2018
Beginner
722 pages
18h 47m
English
Static import allows importing not just a class or an interface but its public members—fields and methods—individually. If you look in one of our test classes, you will see the following static import statement:
import static org.junit.jupiter.api.Assertions.*;
This statement allowed us to write the following:
Person p = new Person("Joe", "Blow", dob);assertTrue(p.equals(p));
That is instead of writing this:
Person p = new Person("Joe", "Blow", dob);Assertions.assertTrue(p.equals(p));
That is one widespread case of static import usage. Another popular case is static importing of constants of an interface or enum. For example, if we have an interface as follows:
package com.packt.javapath.api;public interface Constants { String ...
Read now
Unlock full access