import, static import

  • when using class, can skip package name.

  • inform compiler the package where class is in.

  • command+shift+O : automatically add import statement.

  • class of java.lang package dont need import statement. ex) String, Object, System, Thread ...

import (package).(class);
import (package).*;
  • location : between package declaration and class declaration.

package com.codechobo.book;

import java.text.SimpleDateFormat;
import java.util.*;

public class PackageTest{
    public static void main(String[] args) {
        Date today = new Date();
        SimpleDateFormat date = new SimpleDateFormat("yyyy/mm/dd");
    }
}
  • Add package name if there are same class name and they are in different package.

import java.sql.*;
import java.util.*;

public class ImportTest{
    public static void main(String[] args) {
        java.util.Date today = new java.util.Date();
    }
}
  • static import statement

omitable class name when using static member.

import static java.lang.Integer.*;//all of static member that means all of static variable and static methods
import static java.lang.Math.random;//all of static member of Math class.
import static java.lang.System.out;

out.println(random());
//System.out.println(Math.random());

out.println("Math.PI" + PI);

Last updated