I've got confused. I know that init block executes before constructor, but I don't understand in what order they run separately in Superclass and subclass when code has methods or variables?
You' ve said that in XClass instance blocks run in the order that they appear. It means that
{s="C++";} run first and if it was printed after
String s="Java"; it would have executed after String statement.
Doesn't it contradict the rule which tells that init block executes before constructor and the rest of the code?
Again I'm trying to understand the sequence how does the code executes. Correct me if I'm wrong.
For example we have a class with one init block, and one method. As I know all classes has implicit constructor. It means that when you instantiate an object the code in an init block runs first.
Then JVM executes constructor if it is explicit and has some code inside and only then JMV runs code inside method.
Everything is ok when I look at a YClass. But when I look at the XClass I see that init block runs in the order that they appear, but not before constructor and the rest of the code.