Java FAQ overloaded Constructors

Source: Internet
Author: User


The code is as follows:

public class Example046 {private Example046 (object o) {System.out.println ("Object");} Private Example046 (double[] darray) {//2system.out.println ("double array");} Private Example046 (String str) {//3system.out.println ("string");} public static void Main (string[] args) {new Example046 (null);}}


Output Description:

The above program compiles with the hint "the constructor Example046 (Object) is ambiguous" error, the constructor is ambiguous. If one of the 2 or 3 constructors is removed, it can be executed correctly. The result of the execution is 2 and 3 when the constructor is removed and the remaining one is printed out.


Analysis Summary:

To analyze the above code, you need to understand the parsing process of the Java overload first. The overloaded parsing process for Java is run in two phases. The first stage selects all available and applicable methods or constructors. The second stage selects the most accurate one in the method or constructor selected in the first stage. If a method or constructor can accept any parameter passed to another method or constructor, then we say that the first method lacks precision over the second method. in the preceding code, the 2nd and 3rd constructors are accurate, and 1th is imprecise because it can accept any parameter of 2 and 3. Since there are two precise constructors (2 and 3) in the starting program, the compiler does not know which one to choose, so it will prompt for errors. After removing one of these, the compiler can determine the exact constructor, so it will print.

to invoke the (object) constructor with a null parameter, you need to write the code: New Example046 ((object) null). This ensures that only Example046(Object) is applicable. More generally, to force the compiler to select an exact overloaded version, the actual parameter needs to be transformed to the type declared by the formal parameter.

The above overloaded constructors are not recommended when writing code. You can use static constructors and common factory methods to override overloads. If you do overload, make sure that all overloads accept different parameter types so that no two overloads can be applied at the same time. If this is not possible, then make sure that all applicable overloads have the same behavior .

In summary, the parsing of overloaded versions can be confusing. You should avoid overloading as much as possible, and if you have to overload, then you must follow the above guidelines to minimize this confusion. If a poorly designed API forces you to choose between different overloaded versions, transform the actual parameter into the type of the form parameter for the overloaded version you want to invoke.


note: This "Java Confusion" series are bloggers reading "Java doubts" The original book after the interpretation of the original book and examples of the adaptation and then write a post to publish. All examples are personally tested and shared on GitHub. Use these examples to motivate yourself to benefit others. At the same time, all the posts in this series will be published in the blogger personal public search "Love Ape" or "ape_it" to facilitate reading. If there is any infringement of the original rights of the author, please inform the blogger in time to delete if the reader has objections to the content of the text or questions are welcome through the blog post or public messages and other ways to discuss together.

Source code Address Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

Java FAQ overloaded Constructors

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.