Static Import : Static Import « Language Basics « Java






Static Import

////////Fruits.java

package MyConstants;

public class Fruits {
    public static int apple = 500;
    public static int pear = 501;
    public static int orange = 502;
    public static int banana = 503;
    public static int strawberry = 504;
}


////////////Colors.java
package MyConstants;

public class Colors {
    public static int white = 0;
    public static int black = 1;
    public static int red = 2;
    public static int blue = 3;
    public static int green = 4;
    public static int orange = 5;
    public static int grey = 6;
}



///////////////////////StaticTest.java


import static MyConstants.Colors.*;
import static MyConstants.Fruits.*;

public class StaticTest {
    public static void main(String args[])
    {
        System.out.println("orange = " + orange);
        System.out.println("color orange = " + Colors.orange);
        System.out.println("Fruity orange = " + Fruits.orange);
    }
}



           
       








Related examples in the same category

1.Static import user defined static fields.
2.Java static import: old wayJava static import: old way
3.Use static import to bring sqrt() and pow() into view.Use static import to bring sqrt() and pow() into view.
4.Java static import: System.out and errJava static import: System.out and err
5.Static import: sort
6.Java static import enum