Categories: Java

Sub packages in Java

  • The subpackage is a package within a package.
  • It should be developed to further categorize the package.
  • Sub packages are identical to packages, with the exception that they are defined within other packages.
  • Sub packages are similar to subdirectories, which are directories within directories.
  • Sub packages in themselves are packages, so you have to use/access them similar to packages.
  • They can also contain any types, such as classes, interfaces, and enums.
Example:
package com.coderzpy.code;  
class Simple{  
  public static void main(String args[]){  
   System.out.println("Hello subpackage");  
  }  
}  

To Compile: javac -d . Simple.java

To Run: java com.coderzpy.code.Simple

Output
Hello subpackage
Why do we make Sub Packages in the first place?

Sub packages are used to further categorize and divide a package. It’s comparable to creating a subfolder within a folder to further categorize it so you can better organize your stuff and make it easier to access. A package may contain several sub-packages.

Example of Predefined SubPackage in Java:

The Pattern class of the java.util.regex subpackage is demonstrated in the sample below. Regular expression classes and interfaces are contained in this sub package.

 import java.util.regex.Pattern;
 // To imports all classes of java.util.regex subpackage.
 // import java.util.regex.*; 
 
 class Pattern_program {
   public static void main(String args[]) { 
      // Checks if the given string contains only alphabets
      System.out.println(Pattern.matches("[a-zA-Z]*", "JavaCoding"));
      System.out.println(Pattern.matches("[a-zA-Z]*", "JavaCoding2Coderzpy"));
      // Checks if the given string contains only numbers
      System.out.println(Pattern.matches("[0-9]*", "123456"));
      System.out.println(Pattern.matches("[0-9]*", "123XZY6"));     
   }
 }
Output:
true
false
true
false
Java Static Import:

Java 5’s static import feature allows java programmers to directly access any static member of a class. It’s not necessary to qualify it with the class name.

Note:

  • Static import has the advantage of requiring less coding if you frequently access any static member of a class.
  • Static import’s disadvantage is that it renders your program illegible and unmaintainable if you use it too much.
Example:
import static java.lang.System.*;    
class StaticImportExample{  
  public static void main(String args[]){  
     
   out.println("Hello");//Now no need of System.out  
   out.println("Java");  
  
 }   
}      
Output:
Hello
Java

Note: also read about the Packages in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

11 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago