Home » Java Basics » 03 - Java Installation and Simple Programs
3

A First Java Program

How to write a basic and simple Java class

Traditionally, the first program Java students learn is a program that simply prints the output "Hello World". Implementing this program is also a good way of making sure that your Java installation works. To start, open the newly installed Netbeans 5.5 IDE from the Start Menu.

  1. Go to File -> New Project.
  2. In the first step of the wizard ('Select Project'), click on 'General' on the middle window so that it is highlighted. On the right window, select "Java Application" Click on the 'Next' button.
  3. Choose a folder for the new application. Type the name "MySamples" under project name. Clear the 'Create Main class' checkbox. Click 'finish'.
  4. Now, go to File -> New File. In the 'Choose File Type' window, click on the 'Java' folder (second from top) in the middle window and on 'Java Main Class' in the right window. Click Next
  5. Change the default 'NewMain" name to "HelloWorld". A file will appear for editing:
  6. Remove the following lines of code that represent a constructor.
/** Creates a new instance of HelloWorld */
public HelloWorld() {
}
  1. You will see a function declared with as follows:
public static void main (String[] args) 
  1. Every Java application should have a main method. Execution starts with the main method. The 'public' modifier indicates that any class may access this method. Static makes this method a class method - it has to be invoked using the class name rather than through an instance. The main method may take in input parameters through 'String[] args' (a sequence of strings held in the args variable ). We will not be using this facility. 'Void' implies that the method will not return anything. Main methods always have 'public static void' as part of their signature. The input parameter may be called anything but is usually named 'args'.
  2. Change the line within the main method
            // TODO code application logic here


to

         System.out.println("Hello World!");
  1. Note the neat way in which all the possible choices at each level appears after you type "System". The above line of code evokes the println method of the System class to print a line on the standard output (out).
  2. Change the following words
            @param args the command line arguments


to

         A program that prints "Hello World"
  1. This is a comment ignored by the compiler. Comments may start with "//" and end on the same line, start with a "/*" and end with a */ and encompass many lines, or start with a "/**" and end with a "*/" and encompass many lines. The latter are called documentation comments. Every method in Java should have a documentation comment above it that contains a description of the method and its input and output parameters. These comments may be converted to the method's documentation using the 'javadoc' command.
  2. Now, use File -> to save the file.
  3. Note that you did not need an Import statement to use the 'System' class. This is because System belongs to the core java.lang package.
  4. Next, go to Build -> Compile File. Note that you can also run the compilation by hitting the F9 key.
  5. Wait while the program compiles. A window on the bottom will relay the results of the compilation. This step compiles the Java program using the JVM and creates a class file. The result should look like this:
init:
deps-jar:
Created dir: E:\JavaTutorial\info\MySamples\build\classes
Compiling 1 source file to E:\JavaTutorial\info\MySamples\build\classes
compile-single:
BUILD SUCCESSFUL (total time: 1 second)
  1. Next, click on Run -> Run File and select HelloWorld.java. This step runs your Java program. Once again, the bottom window will show the results of the program. The exact output will appear below "run: ". The "Hello World" string is displayed:
init:
deps-jar:
compile:
run:
Hello World
BUILD SUCCESSFUL (total time: 0 seconds)
  1. Now, minimize NetBeans and open the directory in which you created the new project 'MySamples' in NetBeans. This directory will have the following structure:
Screenshot 3d: MySamples Project Folder
Screenshot 3d: MySamples Project Folder
  1. The 'build' directory (MySamples -> build -> classes) contains the class file HelloWorld.class while the 'src' directory (MySamples -> src) contains the HelloWorld.Java file.
  2. Now, minimize NetBeans and open a dos command window using "cmd" in the Start > Run dialogue. Go to the directory under which the HelloWorld application has been saved.
  3. Type each command as shown in the next screenshot (the commands that allow you to go to the right directory is also shown) and hit return after each command. You should see the “Hello World” string after you type 'Java HelloWorld' and hit return.
  4. As we go on, we will add other sample programs to the MySamples project.
  5. To run a program from any directory, add a new CLASSPATH variable set to the path all the way to 'classes' (E:\JavaTutorial\info\MySamples\build\classes in the example). This should be done following the same procedure used for the "Path" variable. If the classpath is set, you may just type 'java HelloWorld' anywhere to run the program.
Screenshot 3e: Running a Java Program at the DOS Prompt