Let's start talking about the basics of Java coding. We start with classes. In the process we will also take a look at DrJava and maybe BlueJ. Java programs are made out of classes. A class a container of variables and methods. Methods: same as procedures, functions, subroutines, recipes. I will write a program. I will then have to define at least a class. What are classes? How do I use them to model or accomplish things? Not clear yet. Let's open a file with Notepad. We will give it a name: Now.java The extension is not negotiable. The name is, somewhat, depending on circumstances. For us at this stage: totally negotiable. The uppercase is a habit to be explained later. Save and compile. The empty program is valid, etc. No classes no program, let's define classes. Type this in the program: class One { } class Two { } class Three { } Save, and compile: javac Now.java The compilation generates something. It generates a .class file for each class in the program. So in this case One.class, Two.class and Three.class have been created. In the beginning to keep things simple we will have: a) one file per program b) one class per file c) the class name will be the same with the file name That's our simple startup setup. So we may want to create Nine.java with contents: class Nine { } Compile this with: javac Nine.java Out comes: Nine.class Now what? If this is a program we need to be able to start it. To be able to start the program you need to have at least one method. Not just any method. The special method that you need in Java to start the program is called main. class Eleven { public static void main(String[] args) { } } Compile and run this program: javac Eleven.java java Eleven The program runs and does nothing. You can have any number of classes, and a main in each one of them. So Eleven.java can look like this: class Eleven { public static void main(String[] args) { } } class Twelve { public static void main(String[] args) { } } class Thirteen { public static void main(String[] args) { } } You compile it with: javac Eleven.java You can then run: java Eleven or java Twelve or java Thirteen Our One.class, Two.class and Three.class from earlier can't run: java One an error comes up saying no such method main! Now let's make main do something: class Eleven { public static void main(String[] args) { // write your program here System.out.println( "How are \"\" you?" ); } } Here's the code with a lot of thoughts and comments: class Eleven { public static void main(String[] args) { System.out.println( "How are you?" ); // System is a class with useful stuff // classes are containers of methods, variables // our classes so far contain main // out is a variable in System // we use the dot notation to access it // System.out identifies that variable // System.out is the terminal (cmd) on the screen // System.in is the keyboard // System.out is a variable whose value is not an integer // or a string it's a model for something complicated. // System.out is an object. // Objects are (like classes) collections/containers of // variables and methods. } } Now we explore some more: class Eleven { public static void main(String[] args) { System.out.println( "I am Eleven." ); } } class Twelve { public static void main(String[] args) { System.out.println( "I am Twelve." ); System.out.print("I am going to call Eleven now ... "); Eleven.main(args); System.out.println( System.out ); } } There are four types of variables in Java: a) local variables b) parameters These are related to (located in) methods/functions. c) instance variables d) class (static) variables. Class and instance variables don't have to be initialized. Params are initialized when the function is called. Local variables must be initialized before being used. Now let's look at this example: class Eleven { static double m = 3.141592; public static void main(String[] args) { System.out.println( "I am Eleven." ); System.out.println( Twelve.m ); Twelve.m = 4; System.out.println( Twelve.m / Twelve.n ); System.out.println( Eleven.m / Twelve.m ); } } class Twelve { static int n = 3; // class variable static int m; } You compile and run it and the output is: I am Eleven 0 0.785398 Tomorrow we discuss: a) objects b) BlueJ c) DrJava, and d) we start Primitive types in Java: a) integers (int, long, byte, short) b) floating point numbers (double, float) c) booleans (boolean) d) characters (char) Everything else is an object (user-defined type). --