|
1 | | -package com.example; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
5 | | - |
6 | | -/** |
7 | | - * Instance initializer blocks, which Java runs at the start of every constructor that does not |
8 | | - * chain to another constructor of the same class. |
9 | | - */ |
10 | | -public class InstanceInitializers { |
11 | | - private final List<String> names; |
12 | | - private int count; |
13 | | - |
14 | | - { |
15 | | - names = new ArrayList<String>(); |
16 | | - count = 1; |
| 1 | +/// Expect: |
| 2 | +/// - output: "a: count=1 tag=init\nb: count=2 tag=x\nc: count=2 tag=x-y\nd: count=1 tag=init\nstatic=7\n" |
| 3 | +package example; |
| 4 | + |
| 5 | +// Instance initializer blocks run at the start of every constructor that does not chain to another |
| 6 | +// constructor of the same class. A constructor that chains via this(...) must not re-run them. |
| 7 | +public class Program { |
| 8 | + static int staticValue; |
| 9 | + |
| 10 | + static { |
| 11 | + staticValue = 7; |
17 | 12 | } |
18 | 13 |
|
19 | | - public InstanceInitializers() { |
| 14 | + public static class Counter { |
| 15 | + public String tag; |
| 16 | + public int count; |
| 17 | + |
| 18 | + // Two separate initializer blocks, to verify both run and in declaration order. |
| 19 | + { |
| 20 | + tag = "init"; |
| 21 | + } |
| 22 | + |
| 23 | + { |
| 24 | + count = 1; |
| 25 | + } |
| 26 | + |
| 27 | + public Counter() { |
| 28 | + } |
| 29 | + |
| 30 | + public Counter(String first) { |
| 31 | + tag = first; |
| 32 | + count = 2; |
| 33 | + } |
| 34 | + |
| 35 | + // Chains to Counter(String), so the initializers already ran there and must not run again. |
| 36 | + public Counter(String first, String second) { |
| 37 | + this(first); |
| 38 | + tag = tag + "-" + second; |
| 39 | + } |
20 | 40 | } |
21 | 41 |
|
22 | | - public InstanceInitializers(String first) { |
23 | | - names.add(first); |
24 | | - count = 2; |
| 42 | + // A class with an initializer but no declared constructor needs one synthesized, or the |
| 43 | + // initializer would be dropped entirely. |
| 44 | + public static class Implicit { |
| 45 | + public String tag; |
| 46 | + public int count; |
| 47 | + |
| 48 | + { |
| 49 | + tag = "init"; |
| 50 | + count = 1; |
| 51 | + } |
25 | 52 | } |
26 | 53 |
|
27 | | - public InstanceInitializers(String first, String second) { |
28 | | - this(first); |
29 | | - names.add(second); |
| 54 | + public static void describe(String label, int count, String tag) { |
| 55 | + System.out.println(label + ": count=" + count + " tag=" + tag); |
30 | 56 | } |
31 | 57 |
|
32 | | - public int getCount() { |
33 | | - return count; |
| 58 | + public static void main(String[] args) { |
| 59 | + Counter a = new Counter(); |
| 60 | + describe("a", a.count, a.tag); |
| 61 | + |
| 62 | + Counter b = new Counter("x"); |
| 63 | + describe("b", b.count, b.tag); |
| 64 | + |
| 65 | + // count stays 2 rather than resetting to 1, because the chained constructor did not re-run |
| 66 | + // the initializer. |
| 67 | + Counter c = new Counter("x", "y"); |
| 68 | + describe("c", c.count, c.tag); |
| 69 | + |
| 70 | + Implicit d = new Implicit(); |
| 71 | + describe("d", d.count, d.tag); |
| 72 | + |
| 73 | + System.out.println("static=" + staticValue); |
34 | 74 | } |
35 | 75 | } |
0 commit comments