Monday, 7 September 2015

Java Tutorial : Java Sub Package

Master Core Java Concepts!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!

Subscribe Now on YouTube

What Is a Sub-Package in Java?

A package inside another package is called a sub-package. It's created to categorize your packages further so that related classes stay neatly grouped by their specific functionality.

For example, Sun Microsystems defined a main package named java and categorized it into several sub-packages:

  • java.lang: Contains core language classes like System and String.
  • java.util: Contains utility classes like ArrayList and HashMap.
  • java.awt: Contains GUI classes like Button.
  • java.io: Contains input and output classes like File, FileReader, Reader, and Writer.
  • java.net: Contains networking classes like Socket and ServerSocket.

Standard Way of Defining Packages

The standard naming convention for defining packages in Java follows the format: domain.company.package.

  • com.google.util: Here, com is the domain, google is the company, and util is the sub-package.
  • org.amazon.dao: Here, org is the domain, amazon is the company, and dao is the sub-package.

Code Example: Java Sub-Package

// File: HelloWorld.java
package com.google.util;

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}

Key Takeaways

✔️ Better Categorization: Sub-packages break large packages down into smaller, focused groups.

✔️ JDK Structure: You can inspect the JDK's src.zip file to see how java.lang, java.util, and other sub-packages are organized.

✔️ Industry Standard: Always follow the domain.company.package naming pattern for clean, conflict-free code.


Watch More Java Tutorials:

No comments:

Post a Comment

Tutorials