8
Sep

Java Eclipse Tutorial – Part 7: Debugging Java Source Code


Hi. This is Chad (shod) of luv2code.com. Welcome back to another tutorial on Eclipse.
In this video, you will learn how to use Eclipse to debug Java source code. I will cover the
following topics, setting breakpoints, starting the debugger, stepping through a code, viewing
variables, and changing variable values. Okay. Let’s get started. We will learn how
to use debugging with a sample program. I have a program that would generate a list
of random students. The program will also display the students. I’ll run the program
just so you can see the output. As you can see at the bottom, the program simply creates
three students and displays the information to the screen. Before we can debug the code, we must set
a breakpoint. A breakpoint is the line of code the debug at during the debugging session.
In the main method, let’s set a breakpoint on line 20. I’ll “right click”. I’ll say,
“Toggle breakpoint.” Now, let’ start the debugger. Right click
the application and select “Debug As Java Application.” Eclipse will ask you if you
want to use the debug perspective. Select “Yes” because this give us special views that
can aid us in the debugging process. Once the program starts running, it will stop
at the breakpoint we set on line 20. At this point, the program executes this pause. We
can inspect the variables, but there is nothing interesting up there yet. At this point, what I want to do is simply
step over a code. I’m going to use this item here on the toolbar, “Step Over.” I’ll step
over the next line. I’ll step over for the loop. At this point, I actually want to move
into this method, because there’s a method called “Create Random Student,” and I want
to move into it and see the actual internals. To step into the method, Create Random Student,
I will use this toolbar option, “Step Into,” and I’ll select it. Now, this will actually
take me into the Create Random Student method. At this level, I want to step over each one
of these methods. I basically just want to execute this line of code. The one thing I want to draw your attention
to is a section up here at the top for the variable so you’re going to see new variables
pop up with new values. I’ll step over again. That’s our temp first name. We get the last
name index. We get the last name. We get our random age, and then we create a student object. Note here that we have the student object
created. I can expand the student object and I can actually get information about the various
fields that are defined in it. This temp student that was created has the age of 33, first
name was David and last name was Adams. This matches with the variables that were defined
as I was stepping over the code. Then, here, I’ll do one more step. This will
return back to the call. At the result here, I’ll step over one more time. Then I have
the student object that’s created. It also has the same data that was returned from that
method. Let’s walk through the loop one more time.
I’ll just do a step over, and, now, I’ll do a step into. I will step over each one of
these. I will get the temp first name. I’ll get a temp last name, and I’ll get an age. What I’d like to do now is I want to actually
change some values before I actually create the student object. I’ll choose first name
here and I will “right click”. Instead of the name of Claire, I want to change the name
to Daffy and hit “OK.” For last name, instead of Davis, I want to change this value to Duck
and then hit “OK.” Now, we have a student Daffy Duck. Then we will continue with looping
through the example. This will bring us back to this temp student we’ve created and, now,
if I expand it, we can see that this student now has the value of Daffy Duck. At this point, I don’t want to continue stepping
to the code one line at a time. I’d like to simply resume running at normal execution
speed. On my toolbar, I can move up and I could select “Resume.” Notice the output of
the program. We have the three students. The second student is the one that we modified
during the execution, and that’s Daffy Duck, last name, first name. We were successful in debugging our Java Program
with Eclipse. This wraps our video on debugging. You learned how to use Eclipse to set breakpoints
and debug a Java application. Please subscribe to our channel to view more videos on Eclipse
and Java. Click the “Thumbs Up” to like our video. Also, visit our website, luv2code.com
to download the Java source code used in this video.

Tags: , , , , , , ,

5 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *