What is the role of the static keyword in importing the java.lang.System class?

I do not understand the meaning of the static when I import the System class:

 import static java.lang.System.* 

I read a book about Java and it says:

Any import declaration that does not use the word static must begin with the name of the package and end with one of the following:

  • Class name inside this package
  • Asterisk (indicating all classes within this package)

For example, import import java.util.Scanner; valid because java.util is the name of the package in the Java API, and Scanner is the name of the class in the java.util package.

Here is another example. import javax.swing.*; declaration import javax.swing.*; indeed because javax.swing is the name of the package in the Java API, and the asterisk applies to all classes in the javax.swing package.

And I have the following code:

 public class Addition { public static void main(String[] args) { double num; num = 100.53; num = num + 1000; // So when I want to import the whole package java.lang as written in the book, it doesn't work: // import java.lang.*; // or like this: // import static java.lang.*; // NetBeans in both cases doesn't see these abbreviated names `out` and throws errors. Why? out.print("The result is "); out.print(num); out.println(" ."); } } 

And it works when I import this path:

 import static java.lang.System.out; import static java.lang.System.* 

But it does not work when I try to do this:

 import java.lang.System.out; import java.lang.System.* 

What is the meaning of the static in this particular case?

And why import java.lang.*; Doesn't import the whole package with the System class in it?

+6
source share
4 answers

A static import allows you to write this:

 out.print("The result is "); 

instead of this:

 System.out.print("The result is "); 

See http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html .

+11
source

I often use static imports in my unit tests, for example:

 import static org.junit.Assert.*; 

This allows me to write this code:

 assertEquals(2, list.size()); 

Instead of this code:

 Assert.assertEquals(2, list.size()); 
+2
source

Static import is a function introduced in the Java programming language that allows members (fields and methods) defined in a class as public static to be used in Java code without specifying the class in which the field is defined. This feature was introduced in version 5.0 language.

This function provides a type mechanism for including constants in code without reference to the class that originally defined the field. It also helps to abandon the practice of creating a persistent interface: an interface that defines constants, and then writes a class that implements this interface, which is considered an incorrect use of interfaces.

When importing with the static keyword, this means that you somehow inserted it into your class, and you can use its methods in the same way as you call methods of your own classes.

For instance:

 import static java.lang.Math.*; import static java.lang.System.out; 

and:

 out.println("I have a house with an area of " + (PI * pow(2.5,2)) + " sq. cm"); 
+2
source

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; //Works because "out" is a static field b. import static java.lang.System.*; //Works because you are importing ALL static fields and methods inside the System class c. import java.lang.System.out; //Does NOT work because you are trying to import a static field in a non-static way. (see a.) d. import java.lang.System.*; //Actually Works because of the * wildcard which allows you to include all imports. 

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"); 
+1
source

Source: https://habr.com/ru/post/916680/


All Articles