Answer the question
In order to leave comments, you need to log in
How to determine the order of initialization of object properties, depending on different situations and context?
The question is quite simple, as many may think.
Generally:
abstract class A {
int a = 8;
public A() {
show();
}
abstract void show();
}
class B extends A {
int a = 90;
void show() {
System.out.println("" + a);
}
}
public static void main(String args[]) {
new B(); // output - 0
}
class B extends A {
int x = 90; // поменял название переменной!
void show() {
System.out.println("" + a); // 8
}
public A() {
super(); // Вызов конструктора Object`а
a = 8;
show();
}
Answer the question
In order to leave comments, you need to log in
an int variable before initialization has the value 0;
Initialization:
Static fields of class Parent;
Static initialization block of the Parent class;
Static fields of the Child class;
Static initialization block of the Child class;
Non-static fields of the Parent class;
Non-static initialization block of the Parent class;
Parent class constructor;
Non-static fields of the Child class;
Non-static initialization block of the Child class;
Child class constructor.
Those. in the first variant, you display the value of the uninitialized variable of class B, and in the second, the already initialized variable from A.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question