Class Fundamentals


x

Lesson:
Main Points:

  1. The main() method is the normal entry point for Java applications. (As opposed to Applets.)
  2. To execute an application, you type java at the command line, followed by the name of the class containing the main method, followed by any optional arguments.
  3. Java supports variables of two different lifetimes: member and automatic.
  4. Member variables do not have to be initialized since they have default values.
  5. Automatic variables (also known as local variables) do not have default values and therefore must be initialized.

The main() method: So far we have discussed Java applets, here we will introduce Java applications. The examples provided here will be very simple, non-graphical, command-line applications. We will return to applications occassionally throughout this course, but the majority of lessons will involve applets. Here is a really brief example of an applicaton:
public class ex06a{ public static void main(String as[]){ System.out.println("HELLO WORLD"); } }
You complie this file as your would an applet. To run this example type:
  java ex06a

You will notice that no import statements were necessary for this application. You will also notice that the class did not extend anything. The key thing here, though is to notice the main method:
  public static void main(String as[]){

The only things that are optional in this line are the order of static and public and the name you give to the array. The array is used to hold command line arguments. The System.out.println command is just a way to send output to the console.

Now take a look at this example. It requires command line input.

public class ex06b{ static public void main(String myArgs[]){ System.out.println("Argument One: "+ myArgs[0]); System.out.println("Argument Two: "+ myArgs[1]); } }
To run this application type:
  java ex06b California Arizona

Try running it without any arguments. Read the error message you get when you do this.

Here's one more command line example:

public class ex06c{ static public void main(String ARGV[]){ System.out.println("Best " + ARGV[0] + ":"); for(int i=1; i<ARGV.length; i++){ System.out.println(""+i+") "+ ARGV[i]); } } }
To run this example (after you compile it, of course) type:
  java ex06c Fruits apples plums blueberries kiwis

Variables and Initialization:
There are two different types of variables in Java with two different lifetimes:

  1. member variables - These variables are declared outside any method and are accessible from any method in the class.
  2. automatic variables - These variables are created within a method and are accessible only inside the method in which they are created. These variables are more commonly referred to as local variables.

Member variables have default values. These default values are the same as for arrays and so you can flip back to that lesson to see what these default values are. This makes it possible to simply declare a member variable without initializing its value.

With automatic variables, on the other hand, you must initialize their values. If you don't the compiler will complain when you attempt to compile the application or applet. Here's an example of what NOT to do:

  public int willNotWork(){
    int x;
    return x+1; 
  }

Similarly, here's another example of what not to do:
  public int willNotWorkEither(int z){
    int x;
    if(z>0){
      x=10;
    }
    return x;
  }
The compiler will complain here since x might not be initialized depending on the value of z.

Here's a longer example using member and automatic variables:

public class ex06d{ static int z; //member variable declared static public int square(int i){ int s = 0; // automatic variable declared and initialized s = i * i; return s; } //even though we don't take arguments we must have the String array: static public void main(String ARGV[]){ for(;z<9;z++){ //z already equals 0 by default System.out.println(""+z+" squared equals " + square(z)); } } }

x

Assignment:
Write a Java application which shows that you know how to use member and automatic variables. Your application will take any number of arguments (see the example with the for loop). It will display the number of arguments to the third power. You should write a nice little sentence for your output that says something like:
  You entered 5 items.
  5 to the third power is 125.
  Thank you, have a nice day!

x