• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sequence  RSS feed

 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the line by line sequence flow of this program? I do not yet see why it is x y c g
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because x is in a static initializer block (not the go() method) which is run before any methods or constructors, and y is in an instance initailizer block (not the constructor) which is run before any constructors.

A more normal way to arrange the pieces of this class, without changing anything structurally would be:


Note that the order of static initializer blocks and instance initializer blocks in the code doesn't determine their order of execution, except with respect to other initializations of the same kind.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a specific order which different parts of the class are executed in, which you can find in the Java Language Specification, but it is not usually easy to read.
 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Note that the order of static initializer blocks and instance initializer blocks in the code doesn't determine their order of execution, except with respect to other initializations of the same kind.

I am glad to know there is sort of sequence rules, but then when I read this I began to get confused again. (if you reply back I will get to it a bit later, i need to run errand)
 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is a specific order which different parts of the class are executed in, which you can find in the Java Language Specification, but it is not usually easy to read.
Ironic, I just downloaded that pdf file last night and saved it to refer to later, but I may look into it sooner than later. (Thinking what a better progression is.) BTW - I found this example on the Oracle site, I am enrolled in the OCA Fundamentals Training on Demand, but sometimes it seems like I am not learning as I would like to. I cover these things, but when it comes down to following the code, I sometimes experience frustration and panic. (Just being honest)
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the specific order just reveals that class related stuffs are loaded first, then instance.
among the stuffs of the instance, instance initializer is the first one. constructor is the last one. at last, it's not hard to distinguish the order between the method invocation and instance initialization.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Starr wrote:
Jeff Verdegan wrote:I am glad to know there is sort of sequence rules, but then when I read this I began to get confused again.


If you provide details about what's confusing you, it will make it easier for someone to help clear up that confusion.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not quite accurate, because you have forgotten the superclass’ constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!