figuring out the order of execution code in debugger

Posted on 2013-12-12
Last Modified: 2013-12-17
This is a sample question from java certification exam. If I run this in the debuuger of eclipse can I figure out which statment will be executed first

The question asks for output of this. Based on common understanding one would always expect main to execute first and then call the constructor but the output of this is little puzzling (when doing on paper) Vs running the actual code.

So in the backgorund how the interpreter is running this code, how can I figure out that order

public class Sequence 

		System.out.print("c "); 
		System.out.print("y "); 
	public static void main(String[] args) 
	new Sequence().go(); 
	void go() { System.out.print("g "); } 
	static { System.out.print("x "); } 


Open in new window

Question by:anshuma
  • 2
LVL 35

Accepted Solution

mccarl earned 500 total points
ID: 39713807
how the interpreter is running this code, how can I figure out that order
Figuring it out is just a matter of knowing what all the parts are and how they fit together.

one would always expect main to execute first and then call the constructor
Well that is correct in that main would come before the constructor, but as you should have seen in the output, there is something else before that.

For Java to even know what code is in your main() method, it obviously needs to "load" your Sequence class. I'm not talking about instantiating an object of that class but just loading the class into memory from the .class file. This is done the first time the class is referenced, which in this case is when you tell Java to run this class.

Look at line 23, it is called a "static initialization block" and like other static methods/variables it applies to the class as a whole, not any particular instance. And so it actually gets executed just after the class is "loaded", ie. before even the main() method gets called by the JVM. This explains the first output from this code.

Then  the JVM calls your main() method, which the first thing it does it create a new instance of Sequence. Now you probably know that it's a Constructor job to initialize an instance but there is something else. Lines 10 - 13 is called an "instance initialization block" which is similar to above but because it doesn't have the "static" keyword, it applies just to each instance that gets created. It's job is very similar to a constructor, and there is just a few things to remember about them. They get executed _before_ the constructor, and in the case where there is multiple possible constructors, this block gets executed before which ever constructor is invoked. So this should explain the second part of the output.

Then the rest is fairly straight forward, the constructor gets executed after the instance initialization block, giving you your third part of your output. And then back in your main() method, the .go() method is called on the newly created object giving you the last part of your output.

To answer your question on debugging, if you set enough breakpoints through the above code, yes you should be able to follow through the flow of execution. Unfortunately, (to my knowledge) Eclipse has no way to set a breakpoint that stops on "whatever line of code gets executed first" so that is why the best way is to set a number of different breakpoints at all the "possible" lines that may execute first so that you can "catch" the execution as early as possible. Once you do that you can "Step Into" the rest of the code and follow the execution.

Author Closing Comment

ID: 39717426
Thank you very much with this amazing explanation. Any book you recommend that can explain things like this :-)
LVL 35

Expert Comment

ID: 39723616
Not a problem, glad to help! :)

Not sure of any books, I haven't used any for quite a number of years. Any learning that I do these days is purely online.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now