✏️ Explanatory Question

[Packages]

Question:

State the purpose of the following keywords in Java:

(a) package
(b) import

👁 2 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

Answer:

(a) package

The package keyword is used to group related classes and interfaces together into a single namespace.

Purpose:

  • Organizes Java classes properly
  • Avoids name conflicts
  • Provides access protection

Example:

package mypack;

(b) import

The import keyword is used to access classes and interfaces from another package.

Purpose:

  • Allows use of predefined classes
  • Reduces need to write full package names

Example:

import java.util.Scanner;

Now the Scanner class can be used directly in the program.

Conclusion:

  • package → Creates and organizes packages
  • import → Accesses classes from other packages