Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I import static java.lang.System.out.println?

Tags:

java

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.)

like image 503
Anonymous Avatar asked Sep 01 '11 23:09

Anonymous


People also ask

Can you import a static method in Java?

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.

Why is Java Lang not imported?

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.

What library is system out Println?

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 .

Can we use static after import in Java?

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.


1 Answers

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.

like image 75
StriplingWarrior Avatar answered Sep 22 '22 14:09

StriplingWarrior