References to classes with different package names for the same class names in Java __java

Source: Internet
Author: User

There are now two classes with the same name, but the package are different.

Its structure is shown in the following illustration:

The code for two Class A is as follows:

Package pka; public class A {public void PrintA () {System.out.println ("Print:pka. A "); } }

Package PKB; public class A {public void PrintA () {System.out.println ("print:pkb. A "); } }

In addition Main.java for the test class: condition 1:

Call PKA.A in Main

The main code is as follows:

Package pka; public class Main {public static void main (String [] args) {a a=new a (); A.printa ()}}

Test results are:

Print:pka. A

Situation 2:

When we added a pair of PKB. After the import of a, the situation is different.

The main code is as follows:

Package pka; Import PKB. A public class Main {public static void main (String [] args) {a a=new a (); A.printa ()}}

Test results are:

PRINT:PKB. A

At this point through a a=new a (), the creation is not in the package of a, but in the import in the pkb of a.

At this point when we want to create an object in the pka, we must use the following methods:

Package pka; Import PKB. A public class Main {public static void main (String [] args) {pka. A a=new pka. A (); A.printa ();//The result is Print:pka. A}}

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.