Chapter:Classes - main

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

We dedicate a chapter to the main method, since the students have written and seen main methods throughout the course so far, but might feel that it is a strange beast. It is a method, but it seems to be special in the sense that it is called automatically when the program is run. We take the opportunity to talk some more about the main method in this chapter.

Purpose

The purpose is to give the student a deeper understanding of the main method in relation to a Java application. When we first showed a full program in the beginning of the course material, we promised to get back to the main method and explain it further. This is it.

We also talk about the signature (and that it is declared a void method) and that the String args[] parameter allows for passing arguments to the application.

Another purpose is to take the opportunity to discuss blocking in program execution. We teach that a Java application is running only as long as the main method is executing. But if the main method typically is rather short, how come the application might run for a long time? Blocking is the answer. An application may block for input - simply wait for the user to do something.

This leads to mentioning - or at least planting a seed - that applications can be both interactive and non-interactive.

Goal

After this chapter, the student shall understand:

  • the special characteristics and the role of the main method in a Java application
  • what arguments to a Java program are and how to pass arguments to the main method
  • various ways that an application may block

Instructions to the teacher

Common problems

We want to show the students a full program so that we can discuss the main method in relation to a small program. Note that we will stress that programs with only one class and a few static methods called from the main method is not a typical Java application. Make sure that the students understand that small examples are not representative of what larger applications look like.

The main point we want to convey in this lecture is that programs have to start at some point and that's what the main method is - simply an entry point to where the execution begins. As the course material progresses, the students will have more and more tools for making and understanding a full application. They will later realize that the main is just a small part of the application.

Another thing we want to point out, is that students who are new to working with the command line, often struggle to understand the concept of arguments. Have respect for this, and try to make them understand this concept. You can use simple commands to explain what the purpose of arguments are. Start with something simple like ls. When run without arguments, it lists the contents of the current working directory. If you want to list the contents of some other directory, you need to instruct ls of what directory to list. So you provide this information after the name of the command. That is simply put what arguments are. You can then move on to some slightly more complex command like mv. This command is like a transitive verb - it requires arguments in order to make sense. What do you want to move? and where do you want to move it?

Note also that a Java command line consists of the java command, and the argument of the class with the main method. So, students have already used arguments, even if the Java application they have run didn't take any arguments. When providing arguments to a Java application's main method, those arguments come at the very end of the command line, after the class name. This makes Java a little different from "normal" commands.

Java starts the JVM and instructs it where the main method is. Optionally can you pass along arguments to the JVM to pass on to the main method. This might be why some students think arguments are hard to grasp, when learning Java.

If you use an IDE, arguments to the main method is totally different than in command line commands and applications. Typically, the IDE hides the arguments in some configuration dialog which you reach from some menu. Since we don't use IDEs in our course material, this is not a problem. Granted, we have to spend time on discussing arguments in general, but that is skills that the student has use for in more general situations working with the command line. We think this is in line with our reasons for not including IDEs in introductory programming courses.

Starting point of a program

Every program you write must have a starting point, where execution of your code begins. In most languages this is done i a special method (or function as it's called in some languages) called main

Some points which are good to know:

  • There is only one main method per program - the JVM invokes only one
  • Often you put the main method in a separate class, dedicated to simply being "the main class", "the class with the main method"
  • The main method needs to follow a certain signature and form
    • public static void main(String[] args) or else it won't be recognized by the JVM (as a "main method")
  • Usually, the main method only does a few things:
    • check and validate the arguments from the command line (they end up in arg[])
    • create one or a few objects needed to set the application off
    • calls one or a few methods on those objects

In a beginner's book or course, however, often the main method becomes small and self-contained. It often belongs to the one and only class - (and sometimes even is the only method) in the application. This is not at all the way things normally look in a Java application. Normally, a Java application consists of a lot of classes (thousands are not unusual) and the main method is simply the starting point. The first objects are created in main, and then they live on a life of their own, performing the actual work of the application.

In order to give you more realistic expectations of the main method and its relation to the other classes, we often show you a more typical layout even for the simplest application, like HelloWorld:

// HelloMain.java:
public class HelloMain {
  
  public static void main(String[] args) {

    HelloSayer hello;
    if (args.length != 0) {
      hello = new HelloSayer(args[0]);
    } else {
      hello = new HelloSayer();
    }
    hello.greet();
  }
}

//HelloSayer.java:
public class HelloSayer {

  private String name;

  public HelloSayer() {
    name = "user"; 
  }

  public HelloSayer(String name) {
    this.name = name;
  }

  public void greet() {
    System.out.printf("Hello world %s\n", name);
  }
}

/*
Example runs:
$ javac HelloMain.java && java HelloMain
Hello world user
$ javac HelloMain.java && java HelloMain Rikard
Hello world Rikard
$
*/

Writing the main method is something best learned by doing, so please move on to the exercises in the next chapter!

Chapters about classes

Here are the chapters about classes in this book, so that you can keep a check on your progress:

Links

Lecture slides and videos

Further reading

Where to go next

The next page has exercises for this chapter: Classes_-_main_-_Exercises

« PreviousBook TOCNext »