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:
Either way is fine, as long as you are consistent.public class Greeter { public static void main(String[] args) { } }
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
-
Try deleting the semicolon from your
println
statement. Notice that you immediately get a red error marker. If you hover over the marker in the left margin, you can see the error message from the compiler. Eclipse has a very clever incremental compiler that always runs in the background, checking your code for possible syntax errors. If you ever see red error markers in your code, don't bother trying to run it: the results, if any, will be garbage. Fix the syntax errors first! - Try double-clicking on one of the curly braces. Notice that it locates the matching brace and highlights all the code in between.
- Try messing up the indentation, e.g., shift your println statement over to the right, and move the closing brace to the left margin or something. Then highlight the messed-up code, and click Ctrl-I (Cmd-I on a Mac). This is the shortcut for "fix indentation".
- (There is an even more powerful command, Ctrl-F, that will fix up all formatting, not just indentation, according to the formatting preferences you have set. You can find the formatting preferences under Window -> Preferences, or Eclipse -> Preferences on a Mac. Then select Java -> Code Style -> Formatter.)
-
Try highlighting a block of code, then click Ctrl-/ (Cmd-/ on a Mac). This comments out the highlighted code. Highlight the commented-out block and do Ctrl-/ again. This should un-comment it.
There is a keyboard shortcut Ctrl-space (Cmd-space on a Mac) that works for many kinds of code completion. Try this:
- Delete the entire main method
- Within the curly braces for
Greeeter
, typemain
and hit Ctrl-space. Select the first option in the dropdown that pops up. This fills in the declaration for a standard main method - Within the curly braces for your main method, type
syso
and hit Ctrl-space. This expands toSystem.out.println()
;
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.