"Go" Java static import Imports Static

Source: Internet
Author: User

To use static members (methods and variables) We must give the class that provides this static member.

Static import allows static variables and static methods of the imported class to be directly visible in the current class , using these static members without giving their class names.

Static import is also a new feature introduced by JDK5.0, and the following examples illustrate the use of static imports:

  

For example, define a class in a package first:

Package Com.example.learnjava;  Class common{    int age = tenvoid output () {System.out.println ("Hello world!" ); }}


when used in another package , this is used if you do not use a static import:

  

Package com.example.learnjava2;  Import Com.example.learnjava.Common;  Classvoidint a = common.age; System.out.println (a); Common.output (); }}


The import statement was added earlier, the common class is imported , and the static member variable and static method are used to add the class name .

using static import

The syntax for static import is:

  Import static package name. class name. static member variable;

  Import static package name. class name. static member function;

Note the member variables and method names are imported.

After using static import as the previous program:

Package com.example.learnjava2;  Static Com.example.learnjava.Common.AGE;  Static Com.example.learnjava.Common.output;  Classvoidint a = age ; System.out.println (a); Output (); }}

Disadvantages

Excessive use of static imports can reduce the readability of your code to some extent.

Former Wensheng Knight Wind

Original address: http://www.cnblogs.com/mengdd/archive/2013/01/23/2873312.html

"Go" Java static import Imports Static

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.