xhtml
 

Objects

Object oriented programming is a bit complicated, so you have to creep up on it bit by bit. None of the programs we've seen so far have any objects in them, but from now on, they are going to have at least one object, the program object. This will start a good habit, and avoid running into problems later.

The idea is that the main method should always be really short. Its purpose is to deal with any command line arguments, and then launch the world of objects. It is good practice to use it for nothing else. From now on, the basic template for our programs is going to be something like this, with the red bits being the things you change from one program to another.

// Print out an expression

class ProgramName
{
   static public void main (String[] args)
   {
      ProgramName program = new ProgramName();
      program.method();
   }

   void method()
   {
      System.out.println ("expression");
   }
}

The first line of main creates a new program object, and the second line establishes it as the current object while calling the next method. After this, methods are not usually static or public. Also, calls to methods don't have an object name in front, unless you want to change the current object.


Back