Let's break down this code to understand our object.


/**
 * This is my spaceship.
 *
 * @author Jason Petty
 * @version 1.0
 */

This section of code is a comment. A comment is started by typing a /* and ends by typing a */  Everything in the middle is ignored by the compiler when it compiles the program into machine code.


public class SpaceShip
{

This is the code  that actually creates a class.

First, we have the keyword "public". public means that our object can be accessed by any other object. If your spaceship class was private, it would be alone. Who wants to be an object that has no friends?

Second, is the keyword "class". class tells the compiler when it compiles that it is a class. Based on class 0001, a class is basically a recipe for an object.

Third, is the word "SpaceShip". This is the name of  the class. It is also the name of the file that is actually created. The filename and the class name has to match. Notice that the first letter is capitalized. Any class name should start with a capitalized first letter. This is how we, as programmers, know that this is a class when we try to understand someone else's code. I also capitalized the "S" in ship. This is called camel case. When you have two words squeezed together, we camel case the word. This makes the name more readable.

Opening and Closing Curly Braces

You will see a number of opening and closing curly braces. These divide the code into sections. After you type "public class SpaceShip" , you then add an opening curly brace. This means that everything inside the curly brace belongs to the "public class SpaceShip". There should always be a closing curly brace to match any opening curly brace in your class file. If you don't have the proper amount of closing curly braces versus opening curly braces, your program will not compile. As well, if you don't have the proper opening and closing braces to divide your code into sections, it will probably error on compiling. If it doesn't error it is possible that it could have some weird bugs when your code runs. Pay a lot of attention to your opening and closing braces. Study my SpaceShip class to fully understand this concept.


    // instance variables
    private String name;
    private int hitPoints;
    private int shieldEnergy;
    private int ammo;

This is where we define our instance variables. Remember, in class 0001, instance variables are what an object knows about itself. Thus, my spaceship knows it has a name, hit points, shield energy, and ammo. Here we use the java keyword "private". private is the opposite to public. Unlike the definition to define a class as public, here we make our instance variables private. This is very important in the OOP world. Do you remember what OOP stands for? Why is it important? It is important for encapsulation.

The idea of encapsulation is that any object you make while programming in an OOP language should only be modified by the object that owns it. Not by any other object. Let's say you know something about yourself. You have 10 cents. You want to be the one that looses that 10 cents. You don't want anyone else loosing that 10 cents for you. That is the same while programming. You do not want any other object changing any of your instance variables. You are the only one to change your instance variables.

Make all your instance variables private. Later we will discuss getters and setters that handle the concept of changing your private instance variables. Unless there is some really important reason for your instance  variable to be public, make all your instance variables private.

Another comment

Notice we have another comment using two forward slashes. This comment is a one line comment. If you have a short one line comment just use two forward slashes and type your comment. The comment using /* and */ is for "block" comments.

int and String

int is a keyword that stands for "integer" An integer or "whole" number is a number that does not have a fractional component. Examples: 1,2,3, and 4 are all whole numbers or integers. Notice the keyword int is all lowercase.

String is a keyword for a sequence of characters. "Hello World" is a String. "1234" is a String. Notice that a String of characters can be number characters. Another thing to note is that the keyword is spelled with a capital S. Unlike int , String is spelled with a capital S.

Our actual variable names

I gave all my variables names. Variable names should all start with lowercase lettering. You can use camel case lettering if your variable name has two words. You could also use the underscore symbol to separate your words in a variable name. There are naming conventions in Java as to what is a proper for naming your variables. If you name any of your variables improper names, the compiler will complain and you will need  to rename it something else. I might explain the naming convention at some point. You could go look it up on the web if you want to. Me, I just make names up and if the compiler complains, I make a different name until it works. If you really want to know the naming convention, I can assign you homework to discover the naming conventions of Java and post it in the forums. If you don't care , like me. Ignore the homework :)