Grokking Java Interview Questions
0% completed
static, final, and Initialization Order
A program creates one discount of 10 percent and prints it twice, once while the object is being created and once afterward:
in the parent constructor: null 0 percent after the constructor: Holiday 10 percent
The percent field is declared final, so it receives its value only once. Yet the program read two different values from it. How can a final field show 0 first and 10 later?
Answering that takes three facts, which the questions below build in order: what static changes, what final fixes, and when each initializer runs. The fourth question returns to this output.
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%