Java Source Files

In this chapter you will learn:

  1. How is Java source file organized
  2. Example - Java Source

Java Source File

All Java source files must end with the .java extension. A source file should contain, at most, one top-level public class definition. If a public class is present, the class name should match the unextended filename.

Three top-level elements known as compilation units may appear in a file. None of these elements is required.

If they are present, then they must appear in the following order:

  1. Package declaration
  2. Import statements
  3. Class, interface, and enum definitions

Example


package MyPack;//  w  w w . j  a  v a 2 s  . c  om
import java.util.Date;

public class Main {
  public static void main(String args[]) {
    System.out.println(new Date());
  }
}

Next chapter...

What you will learn in the next chapter:

  1. What is Java Enum
  2. How to create enum type in Java
  3. Define enum type variable
  4. Compare two enum values
  5. An enumeration value can also be used to control a switch statement