I'm trying to build character but Eclipse is really confusing.
This is source code written in the Java programming language which models a parent and a child playing a game of catch. Normally this game is played with the parent throwing a ball to their child, who catches it and throws it back, and repeated back-and-forth. The comic title "Bonding" refers to the building of relationship between the parent and the child. The joke lies in the puns using the words
try
,throw
,catch
, andThrowable
. These can refer to actions in the real-life game, but are also keywords in the Java language that are used for exception handling, a method of signaling error conditions and responding to them. Also, the terms "parent" and "child" are usually interpreted more abstractly in programming, as generic terms used in hierarchical data structures.The program, as written, will recursively call the
aim
method alternately on the parent and the child indefinitely, causing each to take turns throwing and catching theBall
object. Note that unlike the real game, this program actually has the same person both throwing and catching the same ball on their turn. The ball is passed onto the other person by aiming it at them, which causes the person to both throw and catch the ball, and aim it back, perpetuating the cycle. This program will also eventually crash with a stack overflow error.The title text refers to the Eclipse IDE, which is a tool commonly used to develop software in Java. "Building character" is something that you would expect a parent to do, in order to instill in his child positive traits, such as confidence and athleticism. This is possibly a reference to Calvin and Hobbes, where Calvin's dad often encourages him to build character in a number of ways, including playing baseball. This is made more likely by other references combining technology with Calvin and Hobbes, such as xkcd comics 409: Electric Skateboard (Double Comic), 702: Snow Tracking and 1002: Game AIs. However, here, "build" might also be a play on the term of "building" a program, while "character" refers to a data type in programming languages. It may also refer to the common notion that programming in C++ or Java builds character due to their powerful but sometimes finicky libraries.
Program description
To compile this Java source code, the two classes would need to be in a .java file. The program defines two classes (types of objects):
- The Ball class extends Throwable, making it possible to use an instance of Ball in exception handling. In English, this means "a Ball is a kind of Throwable object".
- The P class, representing a Person, which contains the following members (attributes):
- a class variable 'target' to point to another P to aim a Ball at.
- a constructor 'P' (in Java the constructor always has the same name as the class) used to create an instance of P and initialize its state (with a target). The keyword this refers to the current instance of P.
- a method 'aim' that takes an instance of Ball named 'ball' as a parameter. This contains the code to actually throw, catch, and pass the ball onto the target.
- a static method 'main' which is called when executing this class. This is the code that sets up the game and starts the process.
The program executes in the following order:
- The static main method is called. It sets up the game by doing the following:
- An instance of P named 'parent' is created without a target (null) using the 'new' keyword.
- Another instance of P named 'child' is created with 'parent' as its target.
- The parent's target is assigned to be the child. Unlike with 'child', setting the parent's target could not be done at the moment when 'parent' was created because its target (the child) has not yet been created at the time. This is why the code for parent and child don't look alike despite this being a symmetrical setup.
- The game begins by having the parent aim a new instance of Ball.
- The aim method first sets up a try block to handle exceptions. A "try" block is required in Java in order to "catch" later.
- Next, the Ball instance 'ball' is thrown. This signals an exception situation and triggers the catch block below.
- In the catch block, the aim method of the target of the P instance is called with the Ball instance (now referred to as 'b').
- The target now executes its own aim method, which is the same code continuing from step 3 except with the current class instance ('this') and its target switched between the parent and the child.