METHODS AND CLASSES
You write all your executable Java code in "methods", and all methods must be grouped into "classes". Optionally, you can group classes into "packages" but we won't do that until later on.
A method is a piece of program code with a name. It can receive any number of input objects or primitives (known as parameters) when it is run, and it can return 0 or 1 object or primitive when it completes running.
A class is a group of methods all of which are concerned with objects of the same type.
If you're already an experienced programmer from some other language, you may think that a method sounds very much like a function, proc, sub, subroutine, macro or command - and you would be right, although there is a little more to it. Java is an Object Oriented language, where you run a can run method ON AN OBJECT - an extra we'll come back to later.
Let's see a first Java class and method, and let's write it in a form that we can run it as if it was a stand alone program:
// Tiniest of programs to check compile / interpret tools
public class Hello {
public static void main(String[] args) {
System.out.println("A program to exercise the Java tools");
}
}
BLOCKS AND STATEMENT STRUCTURE
Within a Java source file, we group together program elements into blocks within curly braces ( {} ) and we'll have blocks nested within blocks.
Our simplest program has two nested blocks - the outer block defines the class (it's called Hello) and the inner block defines a method called main. We could add other methods if we wanted, and they would go inside the outer block, but outside the inner block. There is no practical limit to the number of methods we can define in a class, nor to the length of a block.
Within methods, our executable program code will consist of a series of statements each of which ends with a ; character. Unless the statements specify otherwise, each statement is performed in turn. The ; is mandatory, and you can put as much (or as little) white space as you like into a statement.
DECLARING CLASSES AND METHODS
If you're declaring a class or a method, you have to tell Java the name that you want to associate with the class or method, and how accessible it is to be from elsewhere in your application.
Our class is declared in this example as
public class Hello
which means:
- it's available to any other class (public)
- it's a class (class)
- it's called Hello (Hello)
Our method is declared as
public static void main(String[] args)
which means:
- it's available to run from any other class (public)
- it's not dependent on any particular object (static)
- it doesn't pass anything back to the code that calls it (void)
- it's called main (main)
- It takes one parameter - an array of Strings that it will know as "args"
We'll come back to much of this later - but this combination of keywords and choices just happens to be what the JVM for standalone programs is looking for when it's run - i.e. a static method called main that takes a String array of parameters when it's called. If you vary any part of that specification, then you might still be able to compile correctly but your code won't be runnable as a standalone.
WITHIN A STATEMENT
Each Java executable statement comprises a series of operators and operands:
System.out.println("A program to exercise the Java tools");
println and "." are operators; the string written in double quotes is an operand, as is System.out.
When a statement is run, each of the operators operates on the operands before it, or after it, or on both sides of it (Depending on what the operator is); the result may form an operand which will be acted on further by subsequent operators.
Our first sample statement tells Java to print out a copy of the String that's in the brackets to the System.out channel. println is just one of the thousands of methods that's available as standard in Java 1.4; we suggest you get yourself a good reference book that lists them all in some sort of logical order as there's no way you'll remember them all.
RESERVED WORDS
In our example, words like public and class, static and void are words that the Java Virtual Machine itself understands. Words like main and println are not understood by the JVM, but are never the less unchangeable as they are part of the standard classes and methods provided.
On the other hand, the words "Hello" and "args" are our choice, and we can change them if we wish. You must not use words that the JVM itself understands (a.k.a. "reserved words") for things you name yourself, and it's good advice for you to also avoid using words that relate to standard classes and methods for things you name.
COMMENTING YOUR SOURCE
The final element of our first program is the very first line. It appears to break the rules that we've given so far - it's not in a block, it doesn't seem to have operators, and it doesn't end in a semicolon. It's a comment.
If the Java compiler comes across two slashes when it's "tokenising" the source code, it ignores subsequent text up to the end of the line. This allows you to put reminders of how your code works and what it does into your source. You can also write a comment starting with /* in which case everything up to the following */ will be treated as a comment.
It is VITAL that you comment any programs you write, providing information about what the program does, how it does it, notes of any tricks you've used, etc. Although it may take you a few seconds longer when you're actually writing the program, a few comments well placed can save you and your colleagues hours of heartache later when you come to maintain or enhance the code.
THE CODE IN OPERATION
Having explained the code in depth, let's see the whole program again, and let's compile it, and let's run it:
bash-2.04$ cat Hello.java
// Tiniest of programs to check compile / interpret tools
public class Hello {
public static void main(String[] args) {
System.out.println("A program to exercise the Java tools");
}
}
bash-2.04$ javac Hello.java
bash-2.04$ java Hello
A program to exercise the Java tools
bash-2.04$
A FURTHER PROGRAM
You've seen a lot of "structure" to make up the simplest "hello world" program in Java, but fortunately we can now extend that to provide further functionality at very little cost.
Here's a Java program that uses just the first few features of the Java language that we've told you about to print out a series of lines of text:
/* This Java program will print out a series of lines of
text. The class Two has a main method (where the execution
will start) and several other methods, some of which are
called */
public class Two {
public static void beginner() {
System.out.println("This is my starter");
System.out.println("Still in my starter");
}
public static void main(String[] args) { // Starts execution here
System.out.println("A program to exercise the Java tools");
beginner();
another();
System.out.println("Message from main");
another();
}
public static void noway() { // No calls to this one
System.out.println("This will not happen");
}
public static void another() {
System.out.println("I am here");
}
}
When I run that, I get:
bash-2.04$ java Two
A program to exercise the Java tools
This is my starter
Still in my starter
I am here
Message from main
I am here
bash-2.04$
EXERCISE
Write a Java class with two methods in it - one called "employer" to print out the name of your employer, and the second called "employee" to print out your name. Add an extra method to the class so that you can run the class as a program under the standalone JVM, and add calls to that extra method so that your final result looks like:
bash-2.04$ java Who
Well House Consultants
Graham Ellis
bash-2.04$
See also
Learning to program in Java