OUR STRUCTURE OF JAVA PROGRAMS Remember the structure of a Java program that we know so far? 1. Simplest: one class, one method: public class ____ { public static void main (String[] args) { ... } } 2. One class, multiple methods/functions: We then expanded the structure of a program by allowing multiple functions in a class. So, we now understand how to include multiple functions interacting one another in a class. public class ____ { public static --- () { ... } ... public static void main (String[] args) { ... } // Any number of functions of this form, // one of which is named 'main'. } We then expanded our horizon by learning more things that can go inside a function (method), i.e., conditionals (if...else...), looping (while, for), and arrays. We have been exercising our creativity by combing those things. I used ...... instead of ... to mean that. public class ____ { public static --- () { ...... } ... public static void main (String[] args) { ...... } // Any number of functions of this form, // one of which is named 'main'. } 3. One class, multiple methods, multiple static fields: As you will see in Tic Tac Toe program later, occasionally you will find static fields useful as you program. With static fields introduced into OUR STRUCTURE, it looks like this now: public class ____ { // any number of these (see StaticFields.java for an example) // private static = ; public static --- () { ...... } public static void --- (String[] args) { ...... } // Any number of these functions. // One of these functions is usually named 'main'. } As I mentioned in StaticFields.java, please do not use static fields unless you are absolutely convinced that they are necessary and beneficial to use. If you are not sure, stay away from them. I will tell you more about them as we go. 4. Multiple classes, each class containing multiple STATIC methods, multiple STATIC fields: A program in Java can consist of multiple classes, the kind of classes that we have seen so far, namely a class containing multiple methods and static fields. These multiple classes can be contained in one file or each class contained in its own file. Let's see both possibilities: 4.1 Multiple classes in a single file: public class ____ { } // Any number of classes like this starting WITHOUT the 'public' // keyword. class ____ { } Note that I did not add the 'public' keyword in the second class. If you have multiple classes in a single file, only one class that starts with the keyword 'public' must have the same name as the file name (not including .java). All the other classes in that file obviously will have a different name than the file name. Once you compile the file, then the compiler will create multiple .class files, one for each class in the file. 4.2 Multiple classes in a program, but each class in its own file. In this case you will have a class (with the keyword 'public') in each file, where the file name is spelled the same as the class name (without .java) as usual. How do different classes connect with each other? Well, somewhere inside a class a method will call a method in another class. You will see an example of this when you study Selection.java later that calls the method randomArray(...) defined in class ArrayTools, like this ArrayTools.randomArray(limit, 100); Take a look inside Selection.java. In that example, main in Selection calls a static method randomArray in ArrayTools. When a non-static method is called, the syntax will be different. We have not dealt with non-static methods yet (at least not in a class that we designed ourselves although we have seen some from a class provided by the language such as charAt of String) and we will soon, when we finally start talking about objects! Yeah!!! At that time the structure will yet again expand -:) Suggestion: If multiple classes are closely related to one another to the point that they collectively make up a cohesive unit, then I suggest that you put them in a single file. Otherwise, separate them out into multiple files, specially if one of those files can be used by some other programs as well. In most of our examples, we will use separate files. When we have multiple classes interacting with one another, we need to finally explain what PUBLIC and PRIVATE (and PROTECTED when we talk about inheritance later in the semester) mean. Stay tuned! 5. Multiple classes, each class containing multiple methods (STATIC and NON-STATIC), multiple fields (STATIC and NON-STATIC): Distinction between static and non-static (i.e., dynamic) is important and we will make the distinction when we expand our structure further here.