简体   繁体   中英

Java import class System

I have a question on class imports, It seems you can call a method with a reduced line if you have imported the class. I don't understand what is the name of this operation, and how is it possible...

For instance:

Why 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 also an object named "out"?

Thanks in advance

The variable out will shadow the static import and you will have to use the full name in order to use the function print.

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

yields "cannot invoked print(String) on primitive type int. The same error is shown if out is an object.

What happens is that out from external class must be referenced by full name:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM