OUR STRUCTURE OF JAVA PROGRAMS Remember the structure of a Java program that we have seen 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: Occasionally you will find static fields useful as you program. With static fields introduced into OUR STRUCTURE, the new structure of our Java programs 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 methods, multiple fields (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 static fields private static = ; public static --- () { ...... } ... public static void main (String[] args) { ...... } // Any number of these functions. // One of these functions is usually named 'main'. } // Any number of classes starting WITHOUT the 'public' keyword. class ____ { // any number of static fields private static = ; public static --- () { ...... } ... public static void main (String[] args) { ...... } // Any number of these functions. // One of these functions is usually named 'main'. } 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. 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 fields (STATIC and NON-STATIC) and multiple methods (STATIC and NON-STATIC): Distinction between static and non-static (aka dynamic) is important and we will make the distinction clear soon. But initially I will introduce classes with only dynamic fields and methods first (see 5.1 below). After that, I will present classes that contain both static and dynamic fields and methods all in one class (see 5.2 below). 5.1 Each class with dynamic members (fields and methods) will be in the following form, containing only dynamic fields and methods. I will name this class X, a generic name. public class __X__ { // Any number of dynamic (non-static) fields // They may be public or private, but use private. private ; // Any number of constructors. // Note that the name is identical to the class name. // There is no return type! // They are public. public X () { ... } // Any number of dynamic methods. // They may be public or private. (I will write public here). public --- () { ...... } ... public --- () { ...... } // And usually one static method ('main'), but I will not // include it here - instead I will separate it out to a // different file as we will see immediately below. } public class __UseX__ { // Instead of introducing a main inside class X, we will // create a separate class named UseX that contains one // method, namely main, which we will use to test the // implementation of class X. } Of course, your program can consist of any number of dynamic classes designed this way. UseX for whatever X that you are designing is usually there to test X and after the test is done, it won't be very useful unless the definition for X is modified at which time you would want to update UseX to test the modified portions of X. 5.2 Finally, classes containing multiple fields (static and dynamic), multiple methods (static and dynamic): public class __X__ { //**************************************** // Any number of static fields. // They may be public or private, but use private. private static ; //**************************************** // Any number of static methods. // They may be public or private. public static --- () { ...... } ... public static --- () { ...... } // Usually one of the static methods is named main, // but I will not use it here - instead I will separate // it out to a different file as we will see below. //**************************************** // Any number of dynamic (non-static) fields. // They may be public or private, but use private. private ; //**************************************** // Any number of constructors public X () { ... } //**************************************** // Any number of dynamic methods. // They may be public or private. public --- () { ...... } ... public --- () { ...... } } public class __UseX__ { // Instead of introducing a main inside class X, we will // create a separate class named UseX that contains one // method, main, which is used to test the implementation of // X. } UseX for whatever X that you are designing is usually there to test X and after the test is done, it won't be very useful unless the definition for X is modified at which time you would want to update UseX to test the modified portions of X. Your program can consist of any number of classes designed this way. That's it folks ...!!!... Any program that you will be writing in this course (and most programs that you write beyond this class) can be done with this structure! One exception will be to add a few keywords (e.g., 'extends' or 'implements') later when we talk about inheritance. And, we saw how to 'import' packages.