The BFOIT Introduction to Programming Class
What's a Class?


Introduction

OK, now that you have mastered the "Edit, Compile, Execute" cycle, it's time to find out what all the stuff is that we surround our simple message "Hello World!" with, in order to get it displayed.

When we started working in TurtleTalk, all we had to do was enter commands and things happened.  But with Java, we had to create a class with a main procedure (called a method in Java).  What's with this???

In this lesson, you will learn

  1. what a class is and its syntax,
  2. what Java's keywords are,
  3. what a method is, and its syntax,
  4. about a special method: "public static void main(String [ ] args)"


If Java programs are built from things called classes,
what is a class?

class is a specification (think of it as a blueprint or pattern and a set of instructions) of how to construct something.  Like a blueprint, a Java class has exact specifications.  The programs built from the classes you write match your source code exactly.  Engineers and construction and factory workers use blueprints to build cars, buildings, bridges, virtually anything.  Tailors, seamsters, printers use patterns to make the clothes we wear, books we read.

Start by thinking of a class as an object.  A class can be as simple as a container, like that plastic bottle you drink your favorite beverage from.  Even something this simple has specifications - it has a size.  It can only hold so much.  A company puts something in it.  You take it out.  It's used for storing something.  More commonly, classes can store things and do things.  Think about an MP3 player as an object.  You store songs in it.  But, you can also have it do things, like play a song.  Both of these objects can be represented with Java classes.

You construct a class by writing what looks a lot like an outline.  There are rules that you must follow to write this outline in the Java language.  In computer programming these rules are called the language's syntax

TurtleTalk had its own syntax.  But its syntax is pretty simple, which is one reason I chose to expose you to it before Java.  Java has a much more complex syntax.  The rest of this lesson will cover the rules for writing a syntactically correct Java class.

Why is this important?  If you follow these rules, the Java compiler will not spit out a bunch of errors.  Life will be good.  Trust me...


A class Consists of...

Here is a breakdown of the source code representation of a Java class.  A class can be broken down into two things:
  1. The first piece is a class-header which consists of the keyword "class" and the name you give to the class.  Names in programming languages are also known as identifiers.
  2. The second piece is the body.  It consists of a pair of open/close squiggly brackets with stuff in between them.
Here is a template for the source code of a class:

    
    class <identifier>    
    {
        <member>
        ...
    }
  
Figure 17.1

When you write a class, you replace <identifier> with the name you want to give your class.  <member> and the following line with "..." on it (which means any number of the preceding) is the contents of the body - your specifications of what you want your object to be and do. 

You can use this example every time you write the source code for a class.

Specifically, the keyword "class" should start in the first column of a line followed by the rest of the header information - an identifier in the simplest case.  The open-squiggly-bracket (the first character of the class' body) should be the first character on the first line after this header.  The last line of the file should have a close-squiggly-bracket in column 1.  This signifies the end of the class' body. 

The contents of the body of a class consists of a collection of members.  Members consist of containers called fields (also known as variables) and things it does, called methods (also known as procedures).

The contents of the body (<members>) should be indented at least two columns.  Many programmers indent four columns at a time to make the source code easier to read.  The number of columns you indent is not important, but you need to be consistent with which ever number you choose.  See indentation conventions in the jargon appendix for a longer explanation of why).

There is another convention that you need to be aware of: class identifiers should start with a capital letter and every new word in it should also be capitalized. 

Compare Figure 17.1 with Example 16.1 in the last lesson.  It has a single member, a method declaration.  This method (main) is needed in every application.  It is special, so we'll dedicate the next section to it.


Java Keywords

Java keywords are words reserved as core building blocks of the Java language.  You may not use them for names of things you create, like class, field and method identifiers.  The Java compiler recognizes these words and treats them special.  Table 17.1 contains all of the keywords.  The special meaning of these keywords will be covered as each is introduced in a lesson.

 abstract   const   finally   int   public   this 
 boolean   continue   float   interface   return   throw 
 break   default   for   long   short   throws 
 byte   do   goto   native   static   transient 
 case   double   if   new   strictfp   try 
 catch   else   implements   package   super   void 
 char   extends   import   private   switch   volatile 
 class   final   instanceof   protected   synchronized   while 
Table 17.1


The Special Method: public static void main(String[ ] args)

When we execute a Java application, we use the "java" command which runs the Java Virtual Machine (JVM) and we specify a class file for it to execute.  But, how does the JVM know where to start executing?

ANSWER: It starts with a special method you must name "main";

The source code for main( ) always looks something like:


    public static void main ( String[ ] identifier )    
    {
	statement
        ...
    }
Figure 17.2

I'm going to pass on the full explanation behind why we need "public static" in your declaration of "main" but, trust me, those keywords need to be there. Think of the keywords: public and static as special modifiers needed for main() to be found and accessible for now. 

The identifier can be anything, but it's pretty much a standard that you use the name "args" since this is what everyone uses.


Summary

Java programs are composed of classes.  Classes are composed of members which consist of fields and methods.  Every program needs to have a special method named main.  If you merge Figures 17.1 and 17.2 you end up with the outline for a minimal Java application.  It is:

    
    class <identifier>
    {
          public static void main(String[ ] identifier)    
          {
                statement
                ...
          }

    }
  
Figure 17.3

with identifier as the class' name.

Copy Figure 17.3 onto an index card and keep it handy.  Memorize it!!!

*NOTE* Remember - it is a defacto standard that all class names start with a capital letter, with additional words in it also capitalized.  As an example, MyClass is proper but myClass and Myclass are not.

*NOTE* Remember - your class identifier must also be the name of the file you enter your source code into.  The file also must have the extension of ".java"


Exercises

  1. Compare the Java program we worked on in the last lesson (Example 16.1) with Figure 17.3.  What are the differences?
  2. Write a Java application that displays something (a verse of a song you like, a quote from something you are reading, anything as long as it's a couple of lines long).


Back to Table of Contents
Back to previous Lesson ( What's Java? )
On to next Lesson ( Extending Existing Classes )