You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core.md
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -459,6 +459,52 @@ super.method();
459
459
460
460
В отличии от статических, поля экземпляра класса принадлежат конкретному объекту и могут иметь разные значения для каждого. Вызов метода экземпляра возможен только после предварительного создания объекта класса.
461
461
462
+
Пример:
463
+
```java
464
+
publicclassMainClass {
465
+
466
+
publicstaticvoidmain(Stringargs[]) {
467
+
System.out.println(TestClass.v);
468
+
newTestClass().a();
469
+
System.out.println(TestClass.v);
470
+
}
471
+
472
+
}
473
+
```
474
+
475
+
```java
476
+
publicclassTestClass {
477
+
478
+
publicstaticString v ="Initial val";
479
+
480
+
{
481
+
System.out.println("!!! Non-static initializer");
482
+
v ="Val from non-static";
483
+
}
484
+
485
+
static {
486
+
System.out.println("!!! Static initializer");
487
+
v ="Some val";
488
+
}
489
+
490
+
publicvoida() {
491
+
System.out.println("!!! a() called");
492
+
}
493
+
494
+
}
495
+
```
496
+
497
+
Результат:
498
+
499
+
```
500
+
!!! Static initializer
501
+
Some val
502
+
!!! Non-static initializer
503
+
!!! a() called
504
+
Val from non-static
505
+
506
+
```
507
+
462
508
[к оглавлению](#java-core)
463
509
464
510
## Где разрешена инициализация статических/нестатических полей?
0 commit comments