RHS -> Allan->SWC Notes ->Application Template
Application Template
Version 2 - November 2006

Introduction
This note describes one way of building a Java application.
The purpose is to show a general and easy-to-use template that can be used in most applications (programs) we'll construct on 1st semester.

It is not the most easy way to build small applications. Small and simple applications can be created even more simple than this template!

But it uses elements and structure that has proven to be efficient, reusable and scaleable.
So you can look at this template as an attempt to give you good habits from the start - so you don't have to unlearn bad habits in your carrier!

General architecture
To start a Java application (program) you need to activate the main method.
We define the purpose of this method as to

As the main method needs to be declared inside a class declaration that class will represent the starting point of the complete application.

Using NetBeans
Project and folder name
First thing to decide is the applications project-name - normally the same name as the application (remember that the project-name becomes the name of the folder that holds the project).

Main class
Next thing is to decide whether NetBeans should create a "Main Class" (which is NetBeans term for a class that holds a main method).
If you follow this template you'll select this option and type the name of the application as the name of the class (remember to follow the rules for a class name!).


General example

public class ApplicationName {

 
  //Constructor for the class :
   public ApplicationName() {
     
 //Constructor code goes here
   }

   
//main method creates application
   public static void main(String[] args) {
      
//Initial code (set the stage) goes here

      
//Create application-object :
      ApplicationName app = new ApplicationName();

      
//Give control to application-object by activating a method
      app.runApp();
   }

   
//A method for controlling the application
   public void runApp() {
      
//code for application execution goes here
   }
}

Please note:


Simple example 1

This very simple example uses only one class - the application class (which holds the main method). It simply writes "Hello world" to the screen!

public class HelloWorld {

 
  //Constructor for the class :
   public HelloWorld() {
      //Constructor code (none as this is a very simple example)
   }

   
//main method creates application:
   public static void main(String[] args) {
      //In this very simple example there is no initial code

      //Create application-object :
      HelloWorld app = new HelloWorld();

      
//Give control to application-object by activating a method
      app.runApp();
   }

   
//A method for controlling the application
   public void runApp() {
      
//application code goes here (simply write the message):        
      System.out.println( "Hello World");
   }

}

You might think that the result would be exactly the same if we skipped the method runApp() and moved its single statement to the main() method (replacing the two statements ).

And you would be right!

So why bother with the more complex code?
The simple answer (which actually is important in itself) is that we're separating things: the main() controls initialization (sets the stage) and some other method (runApp() in our example) controls the application.
A more complex answer is that we're getting out of the static method - we won't go deeper into that here!

The principle of separating things is so important and will prove so valuable later that you should accept the (little) complexity and trust that it will pay-off in the future. Of course a simple example as this can't really show how. The next example is slightly more complex so at least the overhead of moving the application code out of the main() method isn't overwhelming

Simple example 2
In this example we have two classes. One that represents a (simple) car and one that represents the application.
The purpose of this application is simply to test the car class. This class should be stored in a file named CarTester.java

public class CarTester {

 
  //Constructor for the class :
   public CarTester() {
     
 //Constructor code (none as this is a very simple example)
   }

   
//main method creates application
   public static void main(String[] args) {
      
 //In this very simple example there is no initial code
 
      
//Create application-object :
      CarTester app = new CarTester();

      
//Give control to application-object by activating a method
      app.runApp();
   }

 
   //A method for controlling the application
   public void runApp() {
      
//application code goes here        
      Car car1 = new Car( "Volvo", "S40", "VC 34 723" );
      Car car2 = new Car( "Toyota", "Corolla", "KH 87 320" );
      System.out.println( car1 );
      System.out.println( car2 );
   }

}

The following class describes the (much too simple) car:

public class Car {

   
//Constructor
   public Car(String brand, String model, String licenseNo) {
      
//Simply copy parameter values into variables
      this.brand = brand;
      this.model = model;
      this.licenseNo = licenseNo;
   }

   
//Method that returns complete state of objects as a String:
   public String toString() {
      return brand + " " + model + " " + licenseNo;
   }

   private String brand;
   private String model;
   private String licenseNo;
}

The files used in this note can be downloaded here:

  ApplicationName.java (for the General example)  
  HelloWorld.java (for the Simple example 1)  
  Car.java, CarTester.java (for the Simple example 2)  

For a similar template for GUI-applications click her: GUI Application Template


Maintained by: Allan Helboe
Updated: 27 March, 2008 11:28