What is static import

What is static import

Answer: static import declaration is similar to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

Example 1 without static import below:

package com.javahonk.staticTest;

public class StaticImportTest {

    public static void main(String[] args) {
	//Without using static import
	double r = Math.cos(Math.PI);
	System.out.println(r);
    }

}

 

Example 2 with static import below:

package com.javahonk.staticTest;

import static java.lang.Math.*;

public class StaticImportTest {

    public static void main(String[] args) {
	//With using static import
	double r = cos(Math.PI);
	System.out.println(r);
    }

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *