Friday, March 11, 2005

Static Import

Introduction

You may import static members of a class using the static keyword (Frens 2004).

SingleStaticImportDeclaration =
'import', 'static', TypeName, '.', Identifier, ';' ;

StaticImportOnDemandDeclaration =
'import', 'static', TypeName, '.', '*', ';' ;

Discussion

The idea is that you no longer need to use the class qualifier in order to access its members (Frens 2004).

import static java.lang.Math.*;

public class StaticImport {
    
    public static void main(String[] args) {
        System.out.println(Math.PI); // not Math.PI
    }
}

References

  • Frens, Jeremy D. (2004). Taming the Tiger: Teaching the Next Version of Java. Retrieved April 17, 2005 from .

Resources

No comments: