Solved

figuring out the order of execution code in debugger

Posted on 2013-12-12
3
236 Views
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 

{ 
	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

0
Comment
Question by:anshuma
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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.
0
 

Author Closing Comment

by:anshuma
Comment Utility
Thank you very much with this amazing explanation. Any book you recommend that can explain things like this :-)
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

728 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

10 Experts available now in Live!

Get 1:1 Help Now