Objects and classes

See also:

Language basics

As mentioned in the first lecture, we assume that you are familiar with some programming language. If that language is not Java, you should read the Java basics tutorial, particularly the sections on Operators, Expressions, statements, and blocks and Control flow statements (including the 6 subsections).

We will not cover this material in lecture.

Locations of variables

At the end of last lecture, we saw that variables correspond to "boxes"; values of primitive types are placed directly into the boxes, while values of Object type are placed elsewhere, and their addresses are written into the boxes:

We start by discussing the different types of variables, and how the boxes are organized in the computer's memory.

There are three kinds of variables, which are stored in three different ways:

local variables

fields and array entries

static variables

Running the debugger

The debugger allows you to step through your code line by line, inspecting data on the stack and heap as it executes.

To start the debugger, first set a breakpoint. A breakpoint tells the debugger where to stop. You can set a breakpoint by double-clicking to the left of your code (in the blue bar, by the line number).

Added breakpoint

Added breakpoint

You then launch the debugger by clicking the button that looks like a bug, next to the run button:

Debugger button

Debugger button

This will start running your program and switch to the debug perspective. When your program reaches a breakpoint, it will stop, allowing you to inspect the variables in the "Variables" tab. You can also see the different stack frames in the "Debug" tab.

The buttons for controlling execution are found in the toolbar:

Debugging control buttons: Disable all breakpoints, Continue execution, Pause execution, Terminate program, Disconnect debugger, Step into, Step over, Step return

Debugging control buttons: Disable all breakpoints, Continue execution, Pause execution, Terminate program, Disconnect debugger, Step into, Step over, Step return

The most useful of these are step over, step into, and step return. These cause the execution to execute the next line, jump into the next function, or jump to the return of the current function, respectively.

Methods

Methods work the same way!

static methods are like static variables; they are part of the class. Only one version of a static method exists.

non-static methods (could be called dynamic methods, but usually called just methods) are like non-static variables (fields); conceptually, at least, there is one copy of the method with each object of the class. We'll talk more about this tomorrow.