WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Core Java · Basic · question 13 of 100

What is a package in Java?

📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

In Java, a package is a way of organizing related classes and interfaces. A package is a collection of classes, interfaces, enums, and subpackages that are grouped together because they are related to each other.

Packages provide a way to avoid naming conflicts between classes in different namespaces. They also make it easier to locate and import classes from other developers or third-party libraries.

To declare a class or interface as part of a package in Java, you include the package statement at the beginning of the source file. For example, if you want to declare a class called MyClass in a package called com.example.myapp, you would include the following statement at the beginning of the source file:


package com.example.myapp;

public class MyClass {
    // Class definition goes here
}

In this example, the MyClass class is declared as part of the com.example.myapp package. This means that it can be accessed from other classes in the same package by simply referencing its class name, or from other packages by using the fully-qualified class name (i.e., com.example.myapp.MyClass).

Java provides several built-in packages, such as java.lang, java.util, and java.io, that contain commonly-used classes and interfaces. You can also create your own packages to organize your own classes and interfaces.

Here’s an example Java program that demonstrates the use of packages:

package com.example.myapp;

public class MyClass {
    public void myMethod() {
        System.out.println("Hello, world!");
    }
}

package com.example.myapp.util;

public class MyUtilClass {
    public static int add(int a, int b) {
        return a + b;
    }
}

import com.example.myapp.MyClass;
import com.example.myapp.util.MyUtilClass;

public class Main {
    public static void main(String[] args) {
        MyClass myObject = new MyClass();
        myObject.myMethod();
        int result = MyUtilClass.add(2, 3);
        System.out.println("Result: " + result);
    }
}

In this program, we define two classes (MyClass and MyUtilClass) in two separate packages (com.example.myapp and com.example.myapp.util, respectively). The MyClass class defines a method called myMethod that prints a message to the console. The MyUtilClass class defines a static method called add that adds two numbers together and returns the result.

In the Main class, we import the MyClass and MyUtilClass classes using the import statement, and then create an object of the MyClass class called myObject and call its myMethod method. We also call the add method of the MyUtilClass class and print the result to the console.

When we run this program, it outputs the following:

Hello, world!

Result: 5

This demonstrates how packages can be used to organize related classes and interfaces in Java, and how classes and interfaces in different packages can be imported and used in other classes.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Core Java interview — then scores it.
📞 Practice Core Java — free 15 min
📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

All 100 Core Java questions · All topics