Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the sequence of execution?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Consider the sample code below from Dan Chisholm,


I want to know what will be the sequence of execution of the above code. I mean, tht static methods are executed in the order in which they are declared??am I right?? If that is the case, then with respect to the above code, m1 should execute first and what will it's parameter be?? I'm confused.
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean that static code blocks (initializers) are always executed in the order they are written?. If not, I dont think there is anything to relate between static methods and static initializers.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m1 executes first, with a String parameter value of "A".

Jothi, have you executed the code (putting in System.out.println's) and read the explanation given by Dan Chisholm?
[ December 08, 2006: Message edited by: Barry Gaunt ]
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry,

My confusion is that, we have 3 static methods. Which one will be the first one to be called at compile time and which one is the first one to be called during run time. Please anyone clarify me on this.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which one will be the first one to be called at compile time


Nothing is going to be called at compile time.

At runtime: main, m1, m2
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just add a few println statements to show where you are?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic