Package Interview Questions:-

  1. What is a package ?

Java packages are a mechanism to group Java classes that are related to each other, into the same "module" (package). When a Java project grows bigger, for instance an app or API, it is useful to split the code into multiple Java classes, and the classes into multiple Java packages.

When you divide classes into multiple Java packages, it becomes easier to figure out where a certain class you are looking for is.

A Java package is like a directory in a file system. In fact, on the disk a package is a directory. All Java source and class files of classes belonging to the same package are located in the same directory.When you divide classes into multiple Java packages, it becomes easier to figure out where a certain class you are looking for is.

Java packages can contain subpackages. Java packages can thus make up what is called a package structure. A Java package structure is like a directory structure. Its a tree of packages, subpackages and classes inside these classes. A Java package structure is indeed organized as directories on your hard drive, or as directories inside a zip file (JAR files).

  2. Creating a Java Package Structure

To create a Java package you must first create a source root directory on your hard disk. The root directory is not itself part of the package structure. The root directory contains all the Java sources that need to go into your package structure.

Once you have created a source root directory you can start adding subdirectories to it. Each subdirectory corresponds to a Java package. You can add subdirectories inside subdirectories to create a deeper package structure.

  3. Adding Classes to Packages

In order to put add Java classes to packages, you must do two things:

1. Put the Java source file inside a directory matching the Java package you want to put the class in.

2. Declare that class as part of the package.

Putting the Java source file inside a directory structure that matches the package structure, is pretty straightforward. Just create a source root directory, and inside that, create directories for each package and subpackage recursively. Put the class files into the directory matching the package you want to add it to.

When you have put your Java source file into the correct directory (matching the package the class should belong to), you have to declare inside that class file, that it belongs to that Java package. Here is how you declare the package inside a Java source file:
package com.jenkov.navigation;

public class Page {

The first line in the code above (in bold) is what declares the class Page as belonging to the package com.jenkov.navigation.

  4. Java Package Naming Conventions

Java packages are always written in lowercase letters. Not like Java classes, where the first letter is usually a capital letter.

To avoid creating packages with the same names as other public Java packages it is recommended that you start your package hierarchy with the reverse domain name of your company.

For instance, since the domain name of my company is I should start with a package structure called com.jenkov. In other words, a top level package named com with a subpackage inside called jenkov

  5. Importing All Classes From Another Package

If you need to use a lot of classes from a certain Java package, importing them one at a time results in a lot of import statements. It is possible to import all classes of a package using the * character instead of a class name. Here is how such an import statement looks:

import anotherpackage.util.*;

  6. Using Classes via Their Fully Qualified Class Name

It is possible to use a class from another package without importing it with an import statement. You can write the fully qualified class name instead of just the name of the class itself.

The fully qualified class name consists of the full package path down to the subclass containing the class, and also including the class name itself. The fully qualified class name is also what you would write in an import statement. For instance:


  7. Built-in Java Packages

The Java platform comes with a lot of built-in Java packages. These packages contain classes for all kinds of purposes that programmers often need, like reading and writing files from the local hard disk, sending and receiving data over networks and the internet, connecting to databases, and many, many other things.


PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language..

Read more