How to use sub-package in Java?


Subpackages are similar to sub-directories. Consider an example. The company had a com.apple.computers package that contained a Dell.java source file, it would be contained in a series of subdirectories like this −

....\com\apple\computers\Dell.java

At the time of compilation, the compiler creates a different output file for each class, interface, and enumeration defined in it. The base name of the output file is the name of the type, and its extension is .class.

For example −

// File Name:Dell.java
package com.apple.computers;
public class Dell {
}
class Ups {
}

Now, compile this file as follows using -d option −

$javac -d.Dell.java

The files will be compiled as follows −

.\com\apple\computers\Dell.class
.\com\apple\computers\Ups.class

You can import all the classes or interfaces defined in \com\apple\computers\ as follows −

import com.apple.computers.*;

Updated on: 04-Feb-2020

822 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements