Organize Code

Source File

Java

 

Defining Source File

Java 8
// Person.java file
class Person {
  
  public String name;
  public int age;
  
}

In Java each Class should be written in a separate file with .java extension.

Java Compiler compiles each .java file into byte-codes in files with extention .class.

In our example, Person class will be compiled into Person.class file.

 

Import Source File

Java 8
// Including source files are handled by JVM

You don't need to specify any code for including external Classes to your source code (except import packages which will be discussed in Namespace Topic).

JVM looks into what is called Classpath to load specific class bytes-codes (.class files).

The ClassPath can be specified at:

  • Compile time
    You can use -cp switch to include your external .class files.
java -cp "/path/to/directory/:/path/to/jarfile.jar:/path/to/classfile.class"
  • Environment Variables
    You can set an Environment Variable CLASSPATH
CLASSPATH=/path/to/directory/:/path/to/jarfile.jar:/path/to/classfile.class