Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

figuring out the order of execution code in debugger

Posted on 2013-12-12
3
Medium Priority
?
242 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 36

Accepted Solution

by:
mccarl earned 2000 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 36

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

704 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