Previous | Next | Trail Map | Writing Java Programs | The Nuts and Bolts of the Java Language


A Simple Class Definition

class Count {
    public static void main(String args[])
        throws java.io.IOException
    {
        int count = 0;

        while (System.in.read() != -1)
            count++;
        System.out.println("Input has " + count + " chars.");
    }
}
In the Java language, all methods and variables must exist within a class. So, the first line of the character-counting application defines a class, Count, that defines the methods, variables, and any other classes needed to implement the character-counting application. Since this program is such a simple one, the Count class just defines one method named main().

Defining a Class in The Anatomy of a Java Application contains a more thorough discussion about defining a class.


Previous | Next | Trail Map | Writing Java Programs | The Nuts and Bolts of the Java Language