Introduction to Classes

5.5 Introduction to Classes

The fundamental unit of Java code is the class. You can think of a class as a collection of related subroutines and variables. Classes provide a large part of the power behind Java, but for now it is probably best just to think of them for now as a way to organize your code. However, you can't get around using classes since every Java "subroutine" is associated with a class.

Our code for finding roots by Newton's method has a single class. The program begins with

class Newton {
and ends with the closing brace } at the end of the file. All class declarations starts with class classname { and end with a }, where classname is the name of the class.

The curly braces enclose the "subroutines" and variables associated with a class. If you look at the example code, you will find inside the Newton class, there are three "subroutines": main, f and fprime.

Finally, a note about file conventions. Each java code file, such as Newton.java, must have a class in it with the same name as the file. For example, our program with the class Newton should be saved in a file called Newton.java.

Next we examine the contents of the Newton class.
Next 5.6 Methods: Functions and Subroutines in Java


David Maxwell, who is still writing this, would like to hear your comments and suggestions.