Questions:
1) works when I import this path:
a. import static java.lang.System.out; b. import static java.lang.System.*
But it does not work when I try to do this:
c. import java.lang.System.out; d. import java.lang.System.*;
2) What is the meaning of a static keyword in this particular case?
3) And why import java.lang. *; Doesn't import the whole package with the System class in it?
----------------------------------------------- --- ------------------------------
Answers:
1) and 2) static imports for example (import static java.lang.System.out) are used to import methods or fields that were declared as static inside other classes in this particular case from the System class.
a. import static java.lang.System.out;
The main reason you want to import methods or fields in a static way is because you can omit the class specification for all calls to these methods or fields. Therefore, instead of writing:
System.out.print("Hello"); System.out.print("World");
write only
import static java.lang.System.* //or import static java.lang.System.out if you only plan on using the 'out' field. out.print("Hello"); out.print("World");
3) import java.lang. * redundant! Java automatically and implicitly imports this package for you! :) , and yes, it imports the System class with it, but do not confuse it, if you do not import it as a static import, you still have to write a long way:
System.out.print("hi");
source share