File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -285,6 +285,88 @@ int fieldValue = (int) field.get(victim);
285285>
286286> → Grandchild non-static block(s) → Grandchild constructor
287287
288+ Пример 1:
289+
290+ ``` java
291+ public class MainClass {
292+
293+ public static void main (String args []) {
294+ System . out. println(TestClass . v);
295+ new TestClass (). a();
296+ }
297+
298+ }
299+ ```
300+
301+ ``` java
302+ public class TestClass {
303+
304+ public static String v = " Some val" ;
305+
306+ {
307+ System . out. println(" !!! Non-static initializer" );
308+ }
309+
310+ static {
311+ System . out. println(" !!! Static initializer" );
312+ }
313+
314+ public void a () {
315+ System . out. println(" !!! a() called" );
316+ }
317+
318+ }
319+ ```
320+
321+ Результат выполнения:
322+
323+ ```
324+ !!! Static initializer
325+ Some val
326+ !!! Non-static initializer
327+ !!! a() called
328+ ```
329+
330+ Пример 2:
331+
332+ ``` java
333+ public class MainClass {
334+
335+ public static void main (String args []) {
336+ new TestClass (). a();
337+ }
338+
339+ }
340+ ```
341+
342+ ``` java
343+ public class TestClass {
344+
345+ public static String v = " Some val" ;
346+
347+ {
348+ System . out. println(" !!! Non-static initializer" );
349+ }
350+
351+ static {
352+ System . out. println(" !!! Static initializer" );
353+ }
354+
355+ public void a () {
356+ System . out. println(" !!! a() called" );
357+ }
358+
359+ }
360+ ```
361+
362+ Результат выполнения:
363+
364+ ```
365+ !!! Static initializer
366+ !!! Non-static initializer
367+ !!! a() called
368+ ```
369+
288370[ к оглавлению] ( #java-core )
289371
290372## Зачем нужны и какие бывают блоки инициализации?
You can’t perform that action at this time.
0 commit comments