Master Core Java Concepts!
Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!
Subscribe Now on YouTubeWhat 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 likeSystemandString.java.util: Contains utility classes likeArrayListandHashMap.java.awt: Contains GUI classes likeButton.java.io: Contains input and output classes likeFile,FileReader,Reader, andWriter.java.net: Contains networking classes likeSocketandServerSocket.
Standard Way of Defining Packages
The standard naming convention for defining packages in Java follows the format: domain.company.package.
com.google.util: Here,comis the domain,googleis the company, andutilis the sub-package.org.amazon.dao: Here,orgis the domain,amazonis the company, anddaois the sub-package.
Code Example: Java Sub-Package
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.
No comments:
Post a Comment