Skip to content

Latest commit

 

History

History
94 lines (71 loc) · 3.8 KB

File metadata and controls

94 lines (71 loc) · 3.8 KB

Practical 2 : Running code within the current project

Structure of a basic Java class

IntelliJ allows you to test and run your code but it expects a particular syntax of the file. In order to run any code on IntelliJ (or any other IDE), you need at least

  • the class declaration
  • the main method

Let's take an example

  • Open the DummyCode.java file

The class

Have a look to line 9 ; a public class is create. In Java, each file correspond to a class, which is the implementation of an object. The class must have the same name as the file, that's why the class is called DummyCode.

The keyword public means that this class is 'visible' outside the file. For example it's possible to create a DummyCode object in the BuggyCode class. On the opposite, if a class, or any methods/functions, has the attribute private, it means that it is only visible in the class itself and no object of this class can be created outside.

The main method

The other mandatory part to make your code running is the main method (line 22). This is the only default runnable method in Java. This method has to be declared as public static void main(final String... args) throws Exception

  • public: see above
  • static: it means that the method can be accessible without creating an object. This is necessary for the main, as it has to be called by IntelliJ
  • void: this is the type of variable returned by this method. A main method doesn't return anything. That's why this keyword is used.
  • final : this keyword is used to create constants. Everything following a final keyword cannot be later modified.

Inside the main, you can then write any code, call methods and create variables. The only restriction if you call methods, which can be a limitation at some point (we'll see later how we can by-pass it), is that all the called methods has to be declared as static.

Running the code

Now we have the class and the main, and within the main some code, let's try to run it.

  • Next to the main class, click on the green play button
  • In the menu, click on Run 'DummyCode.main()'

If the code runs without any bug, you should have the following output

Debugging the code

Let's imagine that you find a bug in your code which is not obvious to solve. To help you, IntelliJ embeds a debugger. In order to make it work, you will need

  • To add breakpoints in your code. For that, click on the line number (on the left of the code). A red dot should appear, and the line should be highlighted in red.
  • Next to the main class, click on the green play button
  • In the menu, click on Debug 'DummyCode.main()'

Note: the debugger has an ant symbol on IntelliJ

Once launched, the code execution will stop at the breakpoint, and you will have access to ALL variables within the current method and their corresponding values.

Different tools are provided by the debugger to help you troubleshooting it

  • Step over: execute the current line and stop to the next
  • Step into: enter inside a method
  • Continue to next breakpoint: execute all lines until the next breakpoint
  • Evaluate expression: enter manually expressions involving some of the current variables/methods and have a look to the results