It seems strange that I can't import static java.lang.System.out.println, when I can import static java.lang.Math.abs. Is there some reason behind this or am I doing something really stupid that I don't see at the moment? (Using Eclipse.)
Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static , to be used in Java code without specifying the class in which the field has been defined.
lang package anytime during running a program? No, java. lang package is a default package in Java therefore, there is no need to import it explicitly. i.e. without importing you can access the classes of this package.
println is a Java statement that prints the argument passed, into the System. out which is generally stdout. System is a class in the java. lang package .
With the help of import, we are able to access classes and interfaces which are present in any package. But using static import, we can access all the static members (variables and methods) of a class directly without explicitly calling class name. The main difference is Readability, ClassName.
Math
is a class, on which abs
is a static method. System.out
is a static field rather than a class. So its println
method isn't actually a static method, but an instance method on a static field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With