Chapter 2 Skills

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


Some Java Syntax for Applications

The shortest application that you can possibly write is:

class ClassName {

public static void main(String args[]) {

the program starts executing here.

}

}


The main Method


Identifiers: Assigning Names


Naming Conventions


Comments

/*
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.
	}
}


Creating and Using Objects from Existing Classes

You must


Look up Information about a class


Object Declaration

To declare an object you must


Object Creation

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?


Garbage Collection

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.


But to confuse you some more ....

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;


Sending a Message

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();


Summary

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


Import Statement

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.


Formatting

class ClassName { public static void main(String args[]) { } }


Software Life Cycle

The process of writing well designed and well implemented code that is easily updated involves a set of well defined stages:


next lecture