Personal tools
You are here: Home Information Systems Java Platform Introduction 6. HelloWorldApp.java examined

6. HelloWorldApp.java examined

Document Actions
  • Send this
  • Print this
  • Content View
  • Bookmarks

HelloWorldApp.java examined

<-- Previous

Table of Contents

Next -->

Defining a class

The lines of code that produce the object blueprint called a class in Java, are called the class definition. All the functionality and attributes of the class are defined in the class definition. A Java class definition must contain the word class and the name of the class. The simplest possible class definition in Java is:

class ClassName {
	...
}

Java is an OOP, and therefore almost everything to do with Java has something to do with an object, or is actually an object. For our purposes every standalone piece of code in Java is a class - including the most simple application such as HelloWorldApp above. This is required because object messaging is the means of communication between code blocks, so in order to be able to work coherently all Java programs must consist of class definitions and their instantiation.

The HelloWorldApp class definition appears at the top of this page. The line:

public class HelloWorldApp {

begins the HelloWorldApp class definition block. This is a simple class, but no matter how simple a class in Java may be, it still has to conform to the rules for the defining of classes.

This class has a single method, viz. the main() method. A main() method contains the code that is used to control and manipulate other objects in order to produce the results you want the program to produce. Every Java program must have a main() method, and it's always defined in the same way:

public static void main(String[] args) {

}

The words public static void comprise the method signature for the main() method:

  1. public means that the method can be called from outside the class, i.e. by another object or some other code such as the Java Interpreter for example.
  2. static indicates that the method is a class method and not an instance method, i.e. there is only one copy of the main() method, which is used every time the main() method is called by any code anywhere.
  3. void indicates that main() doesn't return any value - ever.

How the main() method gets called (used)

When the Java interpreter executes a Java application, the very first thing it does is to call the application's main() method. The main() method then calls all the other methods that are required to make the application function.

If you forget to define a main() method in your program, the compiler will complain and refuse to compile the code. Please note that one application can contain definitions for many classes, but only one class in any application should contain a main() method. This class is the controlling class for that application. HelloWorldApp is a very simple class that in itself is the entire application, and this application calls only one method from another class, namely the println() method of the out object of the System package.

Arguments to the main() method

The main() method accepts a single argument - an array of elements of type String called args. Each string in this array is called a command line element, and these strings are used by the application to affect the way the application runs without requiring the application to be recompiled. In essence, these strings are switches which tell main() how to do its work. For example, you may have written a program that sorts names alphabetically, and the command line element
-descending
would tell main() to sort the names in descending order. You have seen this in action when you ran Java from the console with the command line element -version in order to have the Java version information returned to the screen. The HelloWorldApp class ignores the command line elements, so we won't examine this any further at this point.

Classes and objects in HelloWorldApp

Most Java applications define several objects and contain supporting code that allows the objects to communicate to achieve the desired result. HelloWorldApp is so simple that it has no need to define any extra classes. It does however make use of another existing Java class, the System class in the java.lang package, which is part of the Java API (Application Programming Interface) that is supplied with the Java environment. The System class allows your Java program to interact with whichever operating system your computer is running in a standard way, so that there is no need for different methods to be used to communicate with each different system.

System.out.println("Hello World!");

uses a class variable, viz. the System.out construct. Notice that HelloWorldApp doesn't instantiate the System class, i.e. there is no

System myVar = new System();

instruction. System.out is a class variable and can therefore be called directly from its class without instantiation, because it is associated with the class rather than any instance of the class. System.out is an object variable because it holds an object - an instance of the Java PrintStream object, in fact; println() is a method of the PrintStream object, and this method writes characters to the standard output stream. Usually this results in something being printed on the screen, as it does in our case - the string "Hello World!" (the argument to the println() method) is printed on the screen.

The HelloWorldApp class definition is closed off with a closing brace } , and that's all she wrote. If you are feeling a little confused with all this talk of class variables and instantiation of PrintStream objects, don't worry - you'll get used to the concepts as we go along, and will soon be understanding and using them confidently yourself after a little practice.

<-- Previous

Table of Contents

Next -->

Copyright 2007-2008, by the Contributing Authors. Cite/attribute Resource. 6. HelloWorldApp.java examined. (2008, July 16). Retrieved June 19, 2013, from UWC Free Courseware Web site: http://free.uwc.ac.za/freecourseware/information-systems/java-platform-introduction/helloworldapp.java-examined. This work is licensed under a Creative Commons License : Attribution-ShareAlike 3.0. Creative Commons License : Attribution-ShareAlike 3.0