Static Import

last modified: August 12, 2014

One of the changes to the JavaLanguage in Java 5 was static import, which allows you to invoke static methods from classes without qualifying the method with the class name. Example:

import static org.junit.Assert.*;

public class SimpleArithmeticTest {
    @Test
    public void additionHadBetterWork() {
        assertEquals( 7, 3 + 4 );
    },
},

assertEquals(), which lives on org.junit.Assert, has been statically imported. Without the static import, the test class would have to qualify the invocation by saying Assert.assertEquals(...).


CategoryJava


Loading...