Home >Java >javaTutorial >Example sharing of constructor initialization in Java

Example sharing of constructor initialization in Java

黄舟
黄舟Original
2017-09-15 10:44:431475browse

This article mainly analyzes constructor initialization in Java through examples. The code is very simple and the description is clear. Friends who need it can understand it.

1. Initialization sequence
When Java creates an object, the system first allocates memory for all instance attributes of the object (provided that the class has been loaded), and then the program begins to process these instance attributes. To perform initialization, the initialization sequence is: first execute the initialization block or the initial value specified when declaring the attribute, and then execute the initial value specified in the constructor. Within a class, the order of variable definitions determines the order of initialization. Even if variables are scattered between method definitions, they will still be initialized before any method (including the constructor) is called.


class Window {
  Window(int maker) {
    System.out.println("Window(" + maker + ")");
  }
}
class House {
  Window window1 = new Window(1);
  House() {
    System.out.println("House()");
    w3 = new Window(33);
  }
  Window window2 = new Window(2);
  void f() {
    System.out.println("f()");
  }
  Window w3 = new Window(3);
}
public class OrderOfInitialization {
  public static void main(String[] args) {
    House h = new House();
    h.f();
  }
}

Running result:


Window(1)
Window(2)
Window(3)
House()
Window(33)
f()

As can be seen from the output, the reference w3 will be initialized twice: once in before calling the constructor, and once during the call (the first referenced object will be discarded and garbage collected).

2. Initialization of static data
No matter how many objects are created, static data only occupies one storage area. The static keyword cannot be applied to local variables, so it can only be applied to domains.


class Bowl {
  Bowl(int maker) {
    System.out.println("Bowl(" + maker + ")");
  }
  void f1(int maker) {
    System.out.println("f1(" + maker + ")");
  }
}
class Table {
  static Bowl bowl1 = new Bowl(1);
  Table() {
    System.out.println("Table()");
    bowl2.f1(1);
  }
  void f2(int maker) {
    System.out.println("f2(" + maker + ")");
  }
  static Bowl bowl2 = new Bowl(2);
}

class Cupboard {
  Bowl bowl3 = new Bowl(3);
  static Bowl bowl4 = new Bowl(4);
  Cupboard() {
    System.out.println("CupBoard()");
    bowl4.f1(2);
  }
  void f3(int maker) {
    System.out.println("f3(" + maker + ")");
  }
  static Bowl bowl5 = new Bowl(5);
}
public class StaticInitialization {
  public static void main(String[] args) {
    System.out.println("created new Cupboard() in main");
    new Cupboard();
    System.out.println("created new Cupboard in main");
    new Cupboard();
    table.f2(1);
    cupboard.f3(1);
  }
  static Table table = new Table();
  static Cupboard cupboard = new Cupboard();
}

Run result:


Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
CupBoard()
f1(2)
created new Cupboard() in main
Bowl(3)
CupBoard()
f1(2)
created new Cupboard in main
Bowl(3)
CupBoard()
f1(2)
f2(1)
f3(1)

To a certain extent, initialization is a piece of code that is fixed to be executed. It cannot accept any parameters. Therefore, the initialization process performed by the initialization block on all objects of the same class is exactly the same. For this reason, it is not difficult to find the basic usage of the initialization block. If there is a piece of initialization processing code that is exactly the same for all objects and does not need to accept any parameters, you can extract this initialization processing code into the initialization block.

The above is the detailed content of Example sharing of constructor initialization in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn