Interview I asked you what the static keyword does, if you answer the static modifier variable, the modification method I will think you qualified, answer the static block, I will think you good, answer the static internal class I will think you are very good, answer the static guide package I will be satisfied with you, because I can see that you are very enthusiastic about research technology. Find the following information:
Static Guide package is the static import of Java package, and import static is the new feature in the JDK1.5 instead of import.
Generally we import a class with import com ..... ClassName, while static import is this: import static com ..... Classname.*, there is more static here, there is a class name ClassName behind more. *, which means to import static methods in this class. Of course, you can just import a static method, just change the. * to a static method name. Then in this class, you can call the static method directly with the method name, rather than using the ClassName. Method name.
Benefit: The benefit of this approach is that it simplifies some operations, such as printing operations System.out.println (...); You can write it to a static party
Print (...), direct print (...) when used You can do it. But this method is recommended to use when there are a lot of repeated calls, if only one to two calls, it is better to write directly to the convenience
Example
In Java 5, import statements have been enhanced to provide even more powerful keystrokes, although some argue that this is at the expense of readability. This new feature becomes static import. When you want to use a static member, you can use this feature in both the class in the API and on your own class. The following is a code instance before and after a static import:
Before a static import:
public class Teststatic {
public static void Main (string[] args) {
System.out.println (Integer.max_value);
System.out.println (integer.tohexstring (42));
}
}
After a static import:
Import static java.lang.System.out;
Import static java.lang.integer.*;
public class Teststaticimport {
public static void Main (string[] args) {
Out.println (Max_value);
Out.println (tohexstring (42));
}
}
All two classes produce the same output:
2147483647
2a
Let's take a look at what happens in the code that uses the static import feature:
1. Although this feature is often referred to as "static import," The syntax must be import static, followed by the full limit of the static member you want to import
Name, or a wildcard character. In this example, we do a static import on the Out object of the system class.
2. In this example, we might want to use several static members of the Java.lang.Integer class. The static import statement uses a wildcard character to express "
I want to do a static import on all static members in this class. "
3, now we finally see the benefits of static import features! We do not have to type system in SYSTEM.OUT.PRINTLN. That's great! Other than that
We don't have to type an integer in Integer.max_value. So, in this line of code, we can use shortcuts for static methods and a
A constant.
4, finally, we do more quick operation, this time for the method of the integer class.
We have a little bit of irony about this feature, but not just us. We don't think that saving a small number of keystrokes can make the code
It is difficult to read a bit, but many developers require it to be added to the language.
Here are a few guidelines for using static imports:
You must say import static, cannot say static import.
Beware of ambiguous named static members. For example, if you perform a static import on an integer class and a long class, referencing Max_value will cause
A compiler error because both the integer and long have a Max_value constant, and Java does not know which max_value you are referencing.
You can statically import on static object references, constants (remember, they are static or final), and static methods
Static import of static guide package--java