Reference of classes with similar names and different package names in Java

Source: Internet
Author: User

Now there are two classes with the same name but different packages.

The structure is shown in:

 

 

 

The code for two class A is as follows:

 

Package Ka; <br/> Public Class A {<br/> Public void printa () {<br/> system. out. println ("Print: Ka. A "); <br/>}< br/>}

 

Package PKB; <br/> Public Class A {<br/> Public void printa () {<br/> system. out. println ("Print: PKB. A "); <br/>}< br/>}

 

 

In addition, Main. Java is a test class:

Case 1:

Main

The main code is as follows:

 

Package Ka; <br/> public class main {<br/> Public static void main (string [] ARGs) {<br/> A = new (); <br/>. printa (); <br/>}< br/>}

 

Test results:

Print: Ka.

 

 

 

Case 2:

After we add the import to PKB. A, the situation is different.

The main code is as follows:

 

 

Package Ka; <br/> Import PKB. a; <br/> public class main {<br/> Public static void main (string [] ARGs) {</P> <p> A = new (); <br/>. printa (); <br/>}< br/>}

 

Test results:

Print: PKB.

In this case, a A = new A () is used to create not A in this package, but a in the PKB imported in.

 

In this case, you must use the following method to create the object A in the ka:

Package Ka; <br/> Import PKB. a; <br/> public class main {<br/> Public static void main (string [] ARGs) {<br/> ka. a A = New KA. A (); <br/>. printa (); // The result is print: Ka. A <br/>}< br/>}

 

 

 

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.