Know what it means to:
declare/define a class
import a package
declare an object
create an object
send a message
declare/define a method
format and comment your code
indenting code
garbage collect
The shortest application that you can possibly write is:
class ClassName {
public static void main(String args[]) {
the program starts executing here.
}
}
<modifiers> <return type> <method name> (<parameters>) { <method body> }
For the main method:
modifiers = public static
return type = void (i.e. nothing is returned)
method name = main
parameters = String args[]
valid: Player player_1 Dice LowlyWorker
valid but not good: xy776 H_23_44$$
not valid: 23Player dice# class
valid and all different: Player player plaYer PLAYER
/* Program: DoNothing Author : Jenny Orr Written: Jan 2003 Course : cs231 Description: This is an example of the simplest application program that you can write. It doesn't do anything. */ class DoNothing { // this program does nothing! public static void main(String args[]) { // the program starts executing here. } }
/* /* */ */
The blue /* */ match so that the red */ is treated as not part of the comment. A compiler error will result.
You must
To declare an object you must
<class name> <object name>;
Player player; // declares object
Declaring an object does not create an object. It simply sets up a named location in memory that stores a reference (address) to that object.
The following is WRONG and will generate a compiler error:
Player player; // declares object named player
Player player; // declares object named player again!
But the following is ok because the objects are different:
Player player1; // declares object named player1
Player player2; // declares different object named player2
To create an object you use the syntax
<object name> = new <class name> (<arguments>);
Example: creating a Player object
player = new Player(); // creates object
Declaration and creation can be done all in one step. But remember that there are multiple things going on:
Player player = new Player(); // declares and creates object
What happens first is that all the necessary space in memory is allocated. Then a special method called a constructor is "called" (or invoked or executed) in the Player class. This method takes any arguments that are passed to it and initializes the values of the variables in the object. We will see how to write constructors a little later.
What happens in memory?
You cannot declare an object twice but you can do the following:
player = new Player();
player = new Player();
What does this do?
We say the identifier player is a reference to the object. When an object no longer has a reference it can no longer be accesses. Behind the scenes Java searches for such objects and destroys them. This process is called garbage collection.
The equal sign "=" looks a lot like the = you see in math but it is really quite different. We will explore this more in chapter 3.
Practice: What
happens in memory as the following code is executed?
Player
p1, p2, p3;
p1 = new Player();
p2 = new Player();
p3 = p1;
p1 = p2;
p2 = p3;
To send a message to an object you must specify the name of the object, the name of the message, and the arguments (if there are any).
The syntax is
<object name>.<method name>(<arguments>);
For example
dice.roll(2);
player.takeTurn();
Declare an object
<class name> <object name>;
Create an object
<object name> = new <class name> (<arguments>);
Declare and create simultaneously
<class name> <object name> = new <class name> (<arguments>);
Send a message
<object name>.<method name>(<arguments>);
Example code:
Player player; // declare
player = new Player(); // create
player.takeTurn(); // send message
Some predefined packages are available automatically when you are writing code. However, many are not available without explicity telling Java that we are going to use them. There are several ways to do this.
We are using the java package called javabook that was written by the author of our text. In this package is a class called SketchPad. We can either include the package name everytime we use the class name:
<package name>.<class name>
example to declare a SketchPad object:
javabook.SketchPad myPad;
Or we can just include the line of code at the top of the program:
import <package name>.*;
E.g.
import javabook.*;
....
SketchPad myPad;
That is, to declare an object we need not include the package name.
Packages can contain subpackages. For example, in Lab this week you will be using a class called Graphics which is in the awt subpackage. The awt subpackage is in the java package. The separate the different levels in the package hierarchy, we use a dot:
import java.awt.Graphics;
AWT stands for Abstract Window Toolkit. It contains many classes for creating graphical components such as buttons, textboxes, etc.
"Whitespaces" are the characters: space, tab, line-feed.
class ClassName { public static void main(String args[]) { } }
However, the above code is more difficult to read.
It is important to observe certain conventions when it comes to writing code.
Brackets {} are used to separate or define blocks of code (e.g classes, methods, etc).
{ stuff in block { stuff in inside block } }
Sometimes the first { is placed at the end of the previous line.
Pay close attention to indentation conventions as you learn new instructions.
The process of writing well designed and well implemented code that is easily updated involves a set of well defined stages: