Incremental Java
A Simple Class

Can We Write a Program?

So far, we've looked at bits and pieces of a program. There's nothing that you can do to "run" a program now. This goes to show you how complex a language like Java is. I spend a great deal of time talking about boxes and declarations and balloons and types and expression evaluation and assignment statements, because I believe you need to understand this well before you write your first real program.

But I can't make you wait too long, can I? You'd get impatient. "When can I write a program? I want to see a real Java program!". Patience. Patience. Your waiting has come to an end. We get to write a real program.

This program is, in some ways, not very Java-like. Java is all about programming with objects, and we've barely scratched the surface in learning about objects. All we've said is that an object is a balloon with a handle, and the only objects we've seen so far are Strings.

There's still so much to Java and programming we have yet to see. So we take the next step in our journey by looking at a class.

Classy Classes

What is a class? You may be thinking "I go to school where I take classes". That's not the kind of class we're talking about. Java classes are closer to the classes from biology. In biology, there are categorizations of plants, animals, etc. In particular, the hierarchy is kingdom, phylum, class, order, family, genus, and species.

Think of a class as a category of related things.

In Java, you need to write classes. Java forces this on you. For now, we're going to write a class without truly understanding what a class is used for. The good news is that we'll explain classes and objects very soon.

Syntax

The general structure of a class looks like:
public class Foo
{
  // Class body
}
A class starts off with the words public, then one or more whitespace (remember what whitespace is?), then class, then one or more whitespace, then the name of the class, in this case, Foo.

Then, there is an open brace, then stuff (the class body), then close brace.

We call it a body just like there is a body in a letter or an essay. The body of a letter you write to a friend is the essential part of the letter. There is often a greetings before the body (such as "Dear Sir") and a closing at the end ("Your Truly, Justin"). However, it's the middle part that's important.

Clearly, the stuff (i.e., the class body) is important, and we'll talk about how to put meaningful things in the class body in the next section.

For now, don't worry about what public means. We'll talk about soon enough. You just have to put it there. By now, you have an inkling that we're writing a class, so using class makes sense.

Foo is an identifier. So far, the only identifies we've seen are variable names. Here's the first identifier that refers to something that is not a variable name. Instead it is a class name. The rules for class names is identical to that of variable names. We use the rule for valid identifiers.

Java style says that classes should begin with an uppercase letter. Notice that Java style says that variable names start with a lowercase letter. This make it easier visually to distinguish between variables and classes. If you see an uppercase letter in an identifier, and the programmer has used good Java style, then chances are very good that it is a class.

Recall that from the last section that System is a class.

public static void main( String [] args )

Many teachers of Java hate this. They hate explaning public static void main( String [] args ) because these 6 "words" take pages and pages to explain. They hate it because it distracts from teaching the basics of programming, and forces students to see advanced topics long before they can understand it.

So I hate it too.

But we'll deal.

What is main()? First, let's see where it goes in a class.

public class Foo
{
   public static void main( String [] args )
   {
     // main body
   }
}
main() is a method. To indicate it's a method, I put () at the end. Instead of main, I write main().

Every class is allowed to have one main(). You have to put the 3 words public static void before main, then an open parentheses, then String [] args (the name args can be changed, but most people just leave it as args), then close parentheses.

main() also has a body. It starts with an open brace to start the body, and ends with a close brace, to end the body.

When you "run" a program, you must indicate which class you want to run. For example, we might want to run Foo (as shown above). When this class is "run", it executes the statements in the main() method. This means everything between the open and close brace of main(). (I've written it as a comment // main body above).

When you write a class, copy everything you see above (minus the comments). You can change the name of the class from Foo to whatever you want to name the class.

Your program goes in the main() body. This is where you type Java statements that will do something.

Here's an example of three statements in main().

public class Foo
{
   public static void main( String [] args )
   {
      System.out.println( "Hello, World!" ) ;
      int x = 3, y = 4 ;
      System.out.println( x + " + " + y + " = " + (x + y) ) ;
   }
}
A main() body consists of zero or more statements. main() usually has at least one statement, otherwise it wouldn't do anything.

Let's define a statement.

Statements

So far, statements consists of: Each statement ends in a semicolon to indicate where the statement ends.

When Java runs a program, it runs the first statement, then the second, then the third, and so forth, until the end of the main() body. (Once you learn more about Java, you can control which statements are run and which are not run. We'll get to that in a future lesson).

The example above is interesting. If you ran the program, the output would look like:

Hello, World!
3 + 4 = 7
When the program runs, it runs the first System.out.println(), which prints Hello, World!. Then, it runs the declaration, which doesn't print anything. Finally, it runs the second System.out.println() runnning. (Notice when I write the methods, I don't fill in the arguments. I just write () after the method name---it saves space leaving out arguments).

The second println() method is very interesting. In particular, notice the parentheses around (x + y). Why did I put them there? I wanted to do int addition here. If I left out the parentheses, Java would have done string concatenation instead.

So here's where parentheses are really needed. In general, you add parentheses around an expression when you want to print the evaluated value, instead of each part as a String.

Running a Program

A composer writes a score. A score is several sheets of music. Is the score the same thing as playing the music?

No, it isn't.

Similarly, you write a program. This is like writing a score. Writing a program and running a program are two different things. Running a program is like playing a score.

Your job is to write a program so that when it runs, it does what you want it to do. This is much like the job of a musical composer.