I'm currently self studying Java, and am not sure with this "import" thing. It adds classes(not sure), and has methods that would serve a different function relative on the type of class I declare from the "import"ed thing.
I'm curious if import is frequently used for work, or is this relative to the user if he/she would want to use import or not.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner ThisObject = new Scanner(System.in);
}
}
Java import
is a compile time feature that allows you to omit the package name when programming. There is no byte-code import
, the compiler will replace
Scanner ThisObject = new Scanner(System.in);
with
java.util.Scanner ThisObject = new java.util.Scanner(System.in);
As for how common it is, I would say extremely . However, there is another form of import
called static import
which allows you to bring static
methods and fields from another class into your current namespace . static import
is not very common . Finally, Java variable names start with a lower case letter (by convention).
import static java.lang.System.in;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner thisObject = new Scanner(in); // System.in through static import
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.