Solved

figuring out the order of execution code in debugger

Posted on 2013-12-12
3
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

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

Author Closing Comment

by:anshuma
ID: 39717426
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
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 46
ejb message driven bean mdb creation steps 2 50
glassfish admin console 1 45
dao vs facade design patterns 2 66
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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

739 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