Java Builder Initialization

Source: Internet
Author: User
Tags class definition constructor

The

may consider using the builder to perform the initialization process. This allows for greater flexibility in programming, as we can invoke methods and take action at runtime, thus "onsite" decision initialization values. Note, however, that it is not a hindrance to automatic initialization, which occurs before the builder enters. So, if you use the following code:

Class Counter {
int i;
Counter () {i = 7;}
//...

Then I will first initialize to 0 and then 7. This is true for all basic types and object handles, including those that have been explicitly initialized at the time of the definition. For this reason, the compiler will not try to force us to initialize the elements at any particular place in the builder, or before they are used--initialization is already guaranteed (comment ⑤).

⑤: Instead, C + + has its own list of "builder initial modules" that can be initialized before it enters the builder body, and it is mandatory for objects. See "Thinking in C + +".

1. Initialization order
in a class, the order of initialization is determined by the order in which the variables are defined within the class. Even though variable definitions abound in the middle of the definition of a method, those variables are initialized before any method is called-even before the builder calls. For example:

 

: Orderofinitialization.java
//demonstrates initialization order.

When the constructor are called, to create a
//Tagged object, you'll have a message:
class Tag {
  tag (int marker ) {
    System.out.println ("+ marker +)");
  }

Class Card {
  tag T1 = new tag (1);//before constructor card
  () {
    //Indicate we ' re in the constructor:
   
    system.out.println ("card ()");
    T3 = new Tag (33); re-initialize T3
  }
  Tag t2 = new Tag (2);//after Constructor
  void F () {
    System.out.println ("F ()");
  }
  tag t3 = new tag (3);  At end
} public

class Orderofinitialization {public
  static void Main (string[] args) {card
    t = new Card ();
    T.F (); Shows that construction are done
  }
}///:~
   

In the card, the tag object's definition is deliberately strewn around to prove that all of them are initialized before the builder enters or any other event occurs. In addition, T3 is reinitialized within the builder. Its input results are as follows:

Tag (1) tag (
2)
tag (3) card (
) tag ()
F ()


Therefore, the T3 handle is initialized two times, one time before the builder calls (the first object is discarded, so it can then be taken as garbage). On the surface, it seems inefficient, but it guarantees proper initialization--if an overloaded builder is defined, it does not initialize T3, and there is no "default" initialization in the definition of T3, what happens?

2. Initialization of static data
If the data is static (static), then the same thing happens, and if it belongs to a basic type (the primary type) and is not initialized, it automatically gets its own standard base type initializer, and if it is a handle to an object, So unless you create a new object and connect the handle to it, you get a null value (NULL).
If you want to initialize at the same time as the definition, the method taken is the same as the surface of the non-static value. However, because the static value has only one storage area, no matter how many objects are created, it is inevitable that you will encounter the question of when to initialize that storage area. Here's an example that says a little more clearly:

 

: Staticinitialization.java//specifying initial values in a//class definition.
  Class Bowl {Bowl (int marker) {System.out.println ("Bowl (" + marker +) ");
  } void f (int marker) {System.out.println ("f (" + marker +) ");
  The class Table {static Bowl B1 = new Bowl (1);
    Table () {System.out.println ("table ()");
  B2.F (1);
  } void F2 (int marker) {System.out.println ("F2 (" + marker +) ");
static Bowl B2 = new Bowl (2);
  Class Cupboard {Bowl B3 = new Bowl (3);
  static Bowl b4 = new Bowl (4);
    Cupboard () {System.out.println ("cupboard ()");
  B4.F (2);
  } void F3 (int marker) {System.out.println ("F3 (" + marker +) ");
static Bowl B5 = new Bowl (5); public class Staticinitialization {public static void main (string[] args) {System.out.println ("creating
    New cupboard () in main ");
    New Cupboard ();
    SYSTEM.OUT.PRINTLN ("Creating new Cupboard () in main");
    New Cupboard ();
    T2.F2 (1);
 T3.F3 (1); static Table t2 = new Table ();
static cupboard T3 = new cupboard (); } ///:~


Bowl allows us to examine the creation of a class, while table and cupboard can create static members that are dispersed in the bowl of the class definition. Note Before the static definition, cupboard creates a bowl B3 that is not static. Its output results are as follows:

Bowl (1)
Bowl (2)
Table ()
f (1)
Bowl (4)
Bowl (5)
Bowl (3)
cupboard ()
f (2)
Creating new Cupboard () in main
Bowl (3)
cupboard ()
f (2)
creating new Cupboard () in main
Bowl ( 3)
cupboard ()
f (2)
F2 (1)
F3 (1)


Static initialization occurs only when necessary. If you do not create a Table object and never refer to table.b1 or TABLE.B2, the static Bowl B1 and B2 will never be created. However, they are created only after the first table object is created (or if the first static access occurs). After that, the static object is not reinitialized.
The order of initialization is first static (if they have not been initialized by the previous object creation process), followed by a non static object. We can find the corresponding evidence from the output results.
It is necessary to summarize the process of creating the object here. Consider a class named dog:
(1) When an object of type dog is first created, or the dog class's static method/static field is first accessed, the Java interpreter must find Dog.class (search in the predefined classpath).
(2) After the Dog.class is found (it creates a class object that will be learned later), all its static initialization modules will run. Therefore, static initialization occurs only once-when the class object is first loaded.
(3) When creating a new Dog (), the Dog object's build process allocates enough storage space for a Dog object in the memory heap (HEAP) first.
(4) This storage space is cleared to zero, and all basic types in the dog are set to their default values (0 for numbers, and Boolean and char equivalent settings).
(5) All initialization that occurs when the field is defined is performed.
(6) Execute builder. As will be discussed in chapter 6th, this may actually require a considerable amount of operation, especially when it comes to inheritance.

3. Explicit static initialization
Java allows us to divide other static initialization work into a special "static building clause" (sometimes called a "stationary block") within the class. It looks like the following:

Class Spoon {
  static int i;
  static {
    i = all;
  }
  // . . .


Although it looks like a method, it is actually just a static keyword followed by a method body. As with other static initializations, this code executes only once-when an object of that class is first generated, or when a static member belonging to that class is first accessed (even if no object of that class has been generated). For example:

: Explicitstatic.java
//Explicit static initialization
//with the "static" clause.

Class Cup {
  cup (int marker) {
    System.out.println ("Cup (+ marker +)");
  }
  void f (int marker) {
    System.out.println ("f (" + marker +) ");
  }

Class Cups {
  static Cup C1;
  static Cup C2;
  static {
    C1 = new Cup (1);
    C2 = new Cup (2);
  }
  Cups () {
    System.out.println ("Cups ()");
  }
}

public class Explicitstatic {public
  static void Main (string[] args) {
    System.out.println ("Inside Main ()"); C26/>cups.c1.f ();  (1)
  }
  static Cups x = new Cups ();  (2)
  static Cups y = new Cups ();  (2) 
}///:~


The static initialization module that is used for cups runs when the c1 of a static object is accessed in a row marked (1), or when the row (1) is marked as a comment, and (2) The row is not marked as a comment. if (1) and (2) are marked as annotations, the static initialization process for cups will never occur.

4. Initialization of Non-static instances
For the initialization of non-static variables for each object, Java 1.1 provides a similar syntax format. Here is an example:

: Mugs.java
//Java 1.1 "Instance initialization"

class Mug {
  Mug (int marker) {
    System.out.println (" Mug ("+ marker +"));
  }
  void f (int marker) {
    System.out.println ("f (" + marker +) ");
  }

public class Mugs {
  Mug C1;
  Mug C2;
  {
    C1 = new Mug (1);
    C2 = new Mug (2);
    SYSTEM.OUT.PRINTLN ("C1 & C2 initialized");
  }
  Mugs () {
    System.out.println ("Mugs ()");
  }
  public static void Main (string[] args) {
    System.out.println ("Inside Main ()");
    Mugs x = new Mugs ();
  }
} ///:~


You can see the instance initialization clause:

  {
    C1 = new Mug (1);
    C2 = new Mug (2);
    SYSTEM.OUT.PRINTLN ("C1 & C2 initialized");
  }

It looks very similar to static initialization clauses, except that the static keyword disappears from the inside. In order to support initialization of anonymous inner classes (see chap. 7th), this syntax format must be used.

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.