Initialization is guaranteed by the builder

Source: Internet
Author: User
Tags constructor

For the creation of a method, you can imagine that each class you write is called once Initialize (). This name reminds us that we should make this call first before using the object. Unfortunately, this also means that the user must remember to invoke the method. In Java, the designer of a class can guarantee that each object will be properly initialized because it provides a special method called a builder. If a class has a builder, Java automatically invokes that builder when the object is created-even when the user is unaware. So that's a guarantee!
The next question is how to name the method. There are two aspects of the problem. The first is that any name we use may conflict with the name that is intended to be used for a class member. Second, because the compiler's responsibility is to call the builder, it must know which method to invoke. C + + 's approach seems to be the simplest and more logical, so it is also applied in Java: The builder's name is the same as the class name. This ensures that a method such as this is invoked automatically during initialization.
Here is a simple class with a builder (refer to the "Assignment" section in chapter 3rd if you have problems executing this program).

: Simpleconstructor.java
//demonstration of a simple constructor
package c04;

Class Rock {Rock
  () {/
    * is the constructor System.out.println ("Creating rock");
  }

public class Simpleconstructor {public
  static void Main (string[] args) {for
    (int i = 0; i < i++)
      NE W Rock ();
  }
///:~


Now, once you create an object:
New Rock ();
The appropriate storage space is allocated and the builder is called. This ensures that the object is properly initialized before we can handle it.
Note that the encoding rules for all method initials lowercase do not apply to the builder. This is because the builder's name must be exactly the same as the class name!
Like any other method, the builder can also use the arguments so that we specify how the objects are created. You can easily change the example above so that the builder uses its own arguments. As shown below:

Class Rock {rock
  (int i) {
    System.out.println (
      "Creating rock Number" + i);
  }

public class Simpleconstructor {public
  static void Main (string[] args) {for
    (int i = 0; i < i++)
      new Rock (i);
  }
}

Using the builder's arguments, we can set the corresponding parameters for the initialization of an object. For example, suppose a class tree has a builder that uses an integer argument to mark the height of the trees, so you can create a subtree object as follows:

Tree T = new tree (12); A Tree 12 feet tall

If tree (int) is our only builder, then the compiler will not allow us to create a tree object in any other way.
The builder helps eliminate a large number of classes-related issues and makes your code easier to read. For example, in the preceding code snippet, we do not see an explicit call to the Initialize () method--those methods are conceptually independent of the definition content. In Java, definition and initialization are unified concepts-both are indispensable.
The builder belongs to a more specific method type because it does not have a return value. There is a significant difference between this and the void return value. For void return values, the method itself does not automatically return anything, but it still allows it to return something else. The builder is different, and not only does nothing automatically return, but it doesn't have any choice at all. If there is a return value, and if we can choose to return the content ourselves, then the compiler knows how much to do with that return value.

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.