Skip to content

Commit ae1ff3a

Browse files
KhArtNJavaenhorse
authored andcommitted
Update core.md
1 parent da9ea9b commit ae1ff3a

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

core.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,52 @@ super.method();
459459

460460
В отличии от статических, поля экземпляра класса принадлежат конкретному объекту и могут иметь разные значения для каждого. Вызов метода экземпляра возможен только после предварительного создания объекта класса.
461461

462+
Пример:
463+
```java
464+
public class MainClass {
465+
466+
public static void main(String args[]) {
467+
System.out.println(TestClass.v);
468+
new TestClass().a();
469+
System.out.println(TestClass.v);
470+
}
471+
472+
}
473+
```
474+
475+
```java
476+
public class TestClass {
477+
478+
public static String 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+
public void a() {
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+
462508
[к оглавлению](#java-core)
463509

464510
## Где разрешена инициализация статических/нестатических полей?

0 commit comments

Comments
 (0)