This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the sequence of execution? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is the sequence of execution?" Watch "What is the sequence of execution?" New topic
Author

What is the sequence of execution?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 03, 2002
Posts: 7729
Why don't you just add a few println statements to show where you are?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

Thanks Barry.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What is the sequence of execution?
 
Similar Threads
Stringbuffers
Dan's questions
Doubt in order of exception
String buffer
StringBuffer question