Skip to content

Packages

This content is for Java. Switch to the latest version for up-to-date documentation.

Packages in Java are used to organize related classes and interfaces. They provide a namespace for classes and prevent naming conflicts.

To declare a package, use the package statement at the top of your Java file:

package com.company.project;
public class MyClass {
// Class implementation
}
  • Use lowercase letters
  • Start with your organization’s domain name in reverse (e.g., com.company.project)
  • Package names should be singular
  • Use meaningful and descriptive names

Package structure should mirror directory structure:

  • A class in package com.company.project should be in a directory path /com/company/project/
  1. Avoid importing classes with the same name from different packages
Built with passion by Ngineer Lab