OO Programming with Java


Hello World !

Classes are organized into package(s) that also correspond to sub-directories. As an illustration, the following code defines an 'hello' package that contains both Person and the main application Hello.

  1. Move the source code in an 'hello' directory:
mkdir hello
mv Person.java Hello.java hello/

Add the package declaration at the beginning of each file:

package hello; // PACKAGE declaration

Then, recompile/run with:

javac hello/*.java
java hello.Hello

As a complement, the source files (.java) can be separated from the binaries (.class). The compiler (javac) and the virtual machine (java) then require extra parameters with in particular the classpath (-cp):

  1. Create a src directory and move hello package into it.
    Create an bin directory then recompile/run with:
javac -d bin src/hello/*.java
java -cp bin hello.Hello

9 - 17