Object-Oriented Programming with Java, part I + II

cc

This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.

Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
Adapted for Informatica by: Ruud Hermans

The course is maintained by Technische Informatica Breda


Callstack

When methods call each other, they form what is called a calls stack. This is a list of methods, where each method has been called by the entry over it in the list. The call stack in IntelliJ is shown when the application is paused in the debugger. Concider the following program

public class Main {
    public static void main(String[] args) {
        method1(10);
    }

    public static void method1(int value) {
        method2(value+1);
    }

    public static void method2(int value) {
        method3(value/2);
    }

    public static void method3(int value) {
        System.out.println(value);
    }
}

If we put a breakpoint at the line with the System.out.println code, and look at IntelliJ, we get the following callstack:

callstack

Here you can see the main method called the method1, which in turn called method2, which in turn called method3. You can also see the linenumbers in this callstack, and even click on a line in the callstack to see the variables in that context. By clicking through the different methods, you can see the variable value change in every method, so it is very easy to trace back what happened

Some other debugging features that can help you debug your code with methods are the stepping features

This call stack can also be retrieved while the application is running. Usually you will be viewing this call stack after the program is done running (because the call stack will be printed out), and this is referred to as a back trace or a stack trace. Usually when something goes wrong in your program, java will print out a stack trace