Incremental Java
Behind the Curtain: A Peek at a Java Class

Philosophy

How fast do you want to learn how to program? Right now? Right this moment?

I have a dilemma. I believe in teaching something small, and then building on this, and building on this some more, and voila, you get to a Java program. Unfortunately, the way I originally wrote this tutorial, you'd spend 20 lessons before getting to your first working program.

This is the rough equivalent of teaching someone how to drive by telling them the various parts of the car (steering wheel, accelerator, etc), the safety rules of the road, etc. Halfway through, you may have learned a lot about driving without driving.

If this would help make you a better driver, would you care? Or perhaps it's best to just drive now, and figure things out.

Since I don't know which way is best, I'm going to introduce you to a Java class right now. You won't exactly know what anything means right away, but that's OK. It's going to be painless to learn, and you'll eventually see the pieces fall into place.

We're going to do the equivalent of teaching you how to drive a car, even when you don't know what anything in a car does (pretend you haven't been in a car all your life and have no idea what a steering wheel does or the blinkers do, or anything). In the meanwhile, you'll also be doing the equivalent of learning what a car does. Eventually, the two will meet, and what I write below will suddenly make sense.

For now, prepared to be somewhat confused.

Editing a Class

In Java, you write and use classes. What's a class? Well, you have to wait for now.

Each class has a name. If you write a class, you get to come up with its name. Suppose you pick the name Rectangle.

To write a class, you must use some kind of text editor. A text editor is basically a word processor. It allows you to type your program. A good text editor may even recognize certain parts of Java and do things to make your job of typing the program simpler.

When you write a paper using a word processor, you must name the file. You must also name the file when you edit a Java program.

Java has strict rules when it comes to the name of the file. If the class you are writing is named Rectangle, you must name the file Rectangle.java. If the class you're writing is named BankAccount, then the file must be called BankAccount.java.

The pattern is simple. Whatever you name the class, you must name the file the same name as the class, and add a .java (read that as "dot java") at the end.

Starting a Class Definition

You don't know what a class is, but this is how you start writing a class definition. You edit this in the file Rectangle.java.
public class Rectangle
{
}
Here's what you type: We call everything from public class Rectangle the class definition header.

Everything between the open and close brace is the class definition body. So far, this body has nothing in it.

As practice, write the stuff above, but change the name of the class to BankAccount.

Adding main()

It would be boring to have nothing in the class definition body, just as a letter would be boring if you wrote nothing after Dear Dilbert.

What goes into a class definition? Methods. What's a method? Ah, that's yet another thing you'll learn later on.

We'll put a main() method in. It's shown in navy to make it stand out more.

public class Rectangle
{
   public static void main( String [] args )
   {
      // main's method body
   }
}
The first open brace you see is the start of the class definition body.

Type the following:

As you may notice, there's nothing in the main method body.

Putting a Print Statement in main()

Since main has a blank method body, it doesn't do much. So let's make it print something to the screen.
public class Rectangle
{
   public static void main( String [] args )
   {
      // Printing a message
      System.out.println( "Java Rocks!" ) ;
   }
}
We've added the following line to the main's method body.
      System.out.println( "Java Rocks!" ) ;
Type this accurately. You must use the correct capitalization, dots, double quotes, and semicolon at the end. Any little mistake and your program may not run.

Save this program, and quit out of the editor.

Compiling and Running

A Java program needs to be translated to something your computer can understand. This process of translation is called compiling. Every time you make new changes to your Java program, you need to re-compile.

How do you compile? If you have Java installed correctly, and have a prompt, and work in a UNIX environment (I know, this may not describe your situation), then type:

javac Rectangle.java
This compiles the program. javac (which is java, followed by the letter c) is the name of the compiler. Rectangle.java is the name of your Java file. If it successfully translates, the Java compiler creates a file called Rectangle.class. Your original file is still there, however.

If you have typed something incorrectly, it may print error messages to the screen, and you need to fix it. If this happens, start up the editor on the file, reread your program, and see if you can spot any errors. If you can't, then ask an expert to help out.

Suppose there are no errors. Type the following:

java Rectangle
This time, you only type java. There's no c at the end of this one. You also just type the name of the class. You don't type .java or .class at the end.

If all goes well, you should see:

Java Rocks!
on the screen. Congratulations, you've written your first program.

Trying Again

To see if you've followed what we've said, write a new class called BankAccount (what must the name of the file be?)

This time, change the print message to print My Bank Account Rocks!, and see if you can compile and run it successfully.

But What Did I Do?

Was that fun? OK, so you didn't know what happened or why, but you get a sense of what programming is about. We're now going to slow down some and explain a lot about programming, without doing much programming.

From time to time, we'll stop, write a Java program, and then learn some more.

While programming can be confusing, we just did a quick program to get your feet wet. Don't worry if you don't understand anything quite yet.

For now, be able to write the program above, almost exactly as is, but perhaps changing the print message, and we'll get to explaining what you've done so far.

Right now, you've done the rough equivalent of having someone tell you words to say in French to order some item on a menu. You didn't understand the words. You weren't sure what you're ordering (did you order snails?). You got some food. It's a little progress.

We'll now step back and start telling you about programming a little at a time. The stuff you've written will begin to become clearer and clearer, until you know what's going on.