CO523: Anatomy of a Java program |
[ introduction | classes | attributes | methods ]
This page provides a brief overview of the structure of Java programs.
Complete Java programs are built using classes. These are collections of code 'wrapped-up' under the same (class) name. For instance, Java provides "String" and "System" classes for handling strings and interacting with the system (e.g. program output) respectively. There are a few requirements of classes in Java:
The basic structure of a class, e.g. "MyTestProgram", would be a file ("MyTestProgram.java") with the following contents:
/** * MyTestProgram.java -- a test program * * @author Fred Barnes */ public class MyTestProgram { ... class attributes and methods here }
The first part is a comment -- similar to C and C++, anything between "/*" and "*/" is considered a comment. The additional asterisk on the comment-block start ("/**") and magic "@author" identifier are for the JavaDoc tool, that can be used to automatically generate class documentation from program sources.
The code starts proper at the "public class" definition, with the name of our class ("MyTestProgram"). Inside the braces delimiting the class are attributes (also known as 'fields' and for some 'instance-variables'), and methods ('functions' in C/C++). You may also define further classes inside a class, so called 'inner-classes', whose visibility is limited to the class inside which they are defined -- an approximate correspondance to C is the declaration of a structure within a C file (not a header file).
In order to run the code within a class, it needs to be compiled. If successful, this will generate a Java byte-code file containing the code to be executed (by the JVM -- Java Virtual Machine). For example:
raptor$ ls MyTestProgram.java raptor$ javac MyTestProgram.java raptor$ ls MyTestProgram.java MyTestProgram.class
If there is any error when compiling, it will be reported by 'javac' and no class file will be generated. Be aware of the difference between errors and warnings -- errors are pretty fatal; warnings should be taken notice of, but will not prevent the generation of a class file.
Attributes comprise the 'data' aspects of a class (as opposed to 'code'). Normally this will be data that needs to be accessed by all methods within a particular class. As a typical example:
public class Student { private String name; // student name private String id; // student ID ... methods }
[you may wonder why there are these "... stuff" type things lying around in code; these represent folded blocks, a way of hiding complete chunks of program code within an editor. Few editors actually support this (vim does), yet it is an extremely useful feature..]
The two attributes defined for this "Student" class are "name" and "id", both strings (as denoted by the "String" object type). Furthermore these attributes are declared private; that means they may only be accessed from within the "Student" class. The other alternatives (much like C++) are public and protected. The former indicates that the attribute may be accessed from outside the "Student" class, the latter is related to inheritance (and provides for private sub-class visibility).
Methods are the building blocks of programs in Java, containing real code. They are roughly equivalent to functions in C, C++, Pascal and many other languages. A typical example is:
public static void main (String args[]) { ... method body }
The first line of the code is what is known as a method signature -- this describes everything about a method that other code needs to know in order to call it; just as "extern" declarations do for C. The name of the method here is "main", which is traditionally where the program starts in C and C++ (and also in Java).
[this implies that a program (as distinct from a library) must have a 'main' method, which is certainly true for C and C++. Java blurs this slightly since the name 'main' is really part of the class in which it is defined -- there may be more than one 'main' in a Java program (which can be usefully used for "wrapping up" other Java programs)]
Previous to the method name are various modifiers and the method's return type. In this case the return type is void, meaning that the method returns no value (it just returns). The public modified has the same meaning as it does for attributes -- the name 'main' will be visible to code outside this class (necessary if we intend this 'main' to be the program's starting point).
The other modifier here is static, and this has a non-trivial meaning. Initially, it is sufficient to know that "static" must be used with the 'main' method of a program, and not elsewhere generally. More specifically, "static" indicates that an attribute or method (or sub-class) exists only once and outside of any instance of the class in which it is defined. The term 'instance' relates to the object oriented nature of Java, which is examined a bit on this page.
The last part of the method signature are its parameters, a comma separated list of "type name" style declarations. The 'main' method of a Java program just have the single parameter as shown above -- i.e. an array of "String"s. The parameter names used have little relevance outside the body of the method, but should normally be suitable descriptive. For the 'main' method of a program, the parameter given is an array of strings that hold the command-line parameters of the program.
The body of a method is just a sequence of statements -- and must always be (at the outermost level) enclosed in braces.
Last modified 2nd October 2006 |