1
1
Fork 0

Добавлены примеры кода

This commit is contained in:
Arthur 2020-03-17 14:02:23 +03:00 committed by Enchased Horse
parent 9e718d53e6
commit da9ea9b2c8
1 changed files with 82 additions and 0 deletions

82
core.md
View File

@ -285,6 +285,88 @@ int fieldValue = (int) field.get(victim);
>
> → Grandchild non-static block(s) → Grandchild constructor
Пример 1:
```java
public class MainClass {
public static void main(String args[]) {
System.out.println(TestClass.v);
new TestClass().a();
}
}
```
```java
public class TestClass {
public static String v = "Some val";
{
System.out.println("!!! Non-static initializer");
}
static {
System.out.println("!!! Static initializer");
}
public void a() {
System.out.println("!!! a() called");
}
}
```
Результат выполнения:
```
!!! Static initializer
Some val
!!! Non-static initializer
!!! a() called
```
Пример 2:
```java
public class MainClass {
public static void main(String args[]) {
new TestClass().a();
}
}
```
```java
public class TestClass {
public static String v = "Some val";
{
System.out.println("!!! Non-static initializer");
}
static {
System.out.println("!!! Static initializer");
}
public void a() {
System.out.println("!!! a() called");
}
}
```
Результат выполнения:
```
!!! Static initializer
!!! Non-static initializer
!!! a() called
```
[к оглавлению](#java-core)
## Зачем нужны и какие бывают блоки инициализации?