Declare that a class is part of a package by eg,
package net.grelf.course;
at the very start of the source file, before the class (or interface) declaration and before any imports.
Then, to use the class in another one:
import net.grelf.course.*;
(or, better, give the class name in place of *).
A general java source file is therefore structured as
package declaration (if any)
imports (if any)
class/interface declaration
Best practice: always put your classes and interfaces into packages. Start package names with your own domain name (reversed) to ensure uniqueness.
Convention: package names are always lower case.
The package net.grelf.course would be created in, and read from, a subdirectory of the current directory:
.
|__net
|__grelf
|__course
To make the compiler put a new class into the package in the correct directory you need to include -d baseDir
in the command line. Eg,
javac -d . MyClass.java
(. is the current directory of course) as well as the line at the start of the class source file:
package net.grelf.course;
Then, to run:
java -cp . net.grelf.course.MyClass
(the cp switch sets the classpath, in this case to the current directory.)