Packages (package) in Java

Source: Internet
Author: User

Packages in Java are somewhat similar to C + + namespaces that can be used to prevent class name collisions. Java comes with classes that are located in the Java and Javax package hierarchy, but cannot be imported with import java.* or import javax.*.

When encountering a class with the same name under a few different packages, the package path can be added directly to prevent collisions, such as Java.util.Date date=new java.util.Date Date ();

Static importing of the import static Java.lang.Math , you can directly use the static method of Math, such as sqrt (a).

  Scope of the package:

1. A class, property, or method that is marked as public can be accessed by any class. Marked as private, can only be used to define their class.

2. If this class, attribute, or method is not modified by public or private, it can only be accessed by the class under the same package path.

3. If the property or method of a class is protected decorated, the secondary property or method can be accessed by other classes in the same package, and can be accessed by subclasses in other packages. Protected modification of the situation is more complicated, see protected modification rules.

  Class Conflicts:

The above mentioned that the package has a namespace function, you can prevent the class name conflict. However, in the case of a third-party jar package, it is possible to create a class conflict due to the same full path class defined under the jar package. This type of conflict, which we commonly refer to as jar package collisions, also has a corresponding solution. Jar package conflicts and solutions.

Packages (package) in Java

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.