Adding a main Method

Insert your cursor within the surrounding curly braces ({}) of the Greeter class and add a main method:

public class Greeter {
   public static void main(String[] args) {

   }
}

The meanings of public, static, void, String, and args will be discussed throughout the semester. For now, it is important to know that the main method is where your program will begin its execution.

Note that this method is indented one level within the class. You can use the Tab key to indent the first line. Eclipse should assist in formatting the rest. Your code will be more readable by yourself and your graders if you cleanly indent as you add methods and other blocks of code.

Many Java programmers prefer to keep the braces lined up vertically:

public class Greeter 
{
   public static void main(String[] args) 
   {

   }
}
Either way is fine, as long as you are consistent.

Be sure to save your work each time you make a change, using Ctrl-S or File -> Save.

Printing

Add a single line within the curly braces of the main method that prints out the text "Hello, NAME", where NAME is replaced by your name, for example,

System.out.println("Hello, Steve");

System.out is an object that represents the standard output stream for printing to a text window (sometimes called a "console"). This object has an operation, or method, named println that actually does the work.

Running Your Program

Provided you typed correctly, you have just completed your first Java program. To run it, right-click on the Greeter class in the Package Explorer and select Run As -> Java Application. (If the editor pane containing the Greeter class is active, the green Play button in the toolbar should also work.) You should see the printed text in Eclipse's console window.

A few useful things to know about Eclipse

This is a Program?

Most "programs" you are familiar with have buttons, menus, graphics, and so on. For most of this course, your perfectly valid code will consist of components with well-defined behavior that are intended to form part of a larger system. Sometimes, we'll use them to make a complete program that involves input and output only on a text console. Graphical user interfaces (GUIs) are an advanced topic that we will not address in great detail in this course. In some of our assignments you'll be provided with a GUI to work with, and you are encouraged to explore how they work.