Java Class Import System

I have a question about importing classes. It seems you can call the reduced line method if you imported the class. I do not understand what this operation is called, and how it is possible ...

For instance:

Why is this code

public class test { public static void main (String args[]) { System.out.print("Test"); } } 

Can be replaced by

 import static java.lang.System.out; public class test { public static void main (String args[]) { out.print("Test"); } } 

What happens if you have an object named "out"?

Thanks in advance

+6
source share
2 answers

What happens is that the full name must be specified from the outer class:

 String out = "Hello World"; java.lang.System.out.println(out); 
+3
source

The output of the variable will obscure the static import, and you will need to use the fully qualified name to use the print function. A.

 import static java.lang.System.out; public class Tester5 { public static void main (String args[]) { int out=0; out.print("Test"); } } 

yields "cannot call print (String) in the primitive int type. The same error is displayed if out is an object.

+5
source

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


All Articles