JAVA import statement 1| Package in Java | FewMinutesWithAnand | OCP 17

Поделиться
HTML-код
  • Опубликовано: 16 май 2024
  • In Java, packages and import statements are fundamental to organizing and using code from different classes and libraries. Here’s a detailed explanation of how packages and import statements work in Java:
    Packages
    A package in Java is a namespace that organizes a set of related classes and interfaces. Conceptually, you can think of it as a folder or directory that holds various class files.
    #### Creating a Package
    To create a package, you use the `package` keyword at the very beginning of a Java source file. The convention is to use a reverse domain name to avoid name conflicts, for example:
    ```java
    package com.example.myapp;
    ```
    This line declares that the class defined in this file belongs to the `com.example.myapp` package.
    #### Using Packages
    When you create a package structure, your source files should be stored in directories that match the package structure. For instance, a class declared to be in `com.example.myapp` should be placed in a directory structure like this:
    ```
    src
    └── com
    └── example
    └── myapp
    └── MyClass.java
    ```
    Import Statements
    Import statements are used to bring other classes and interfaces into visibility. This allows you to use classes from other packages without having to use their fully qualified names every time.
    #### Basic Import
    To import a specific class, you use the `import` keyword followed by the fully qualified name of the class:
    ```java
    import java.util.List;
    ```
    This statement allows you to use the `List` class directly without prefixing it with `java.util.` every time.
    #### Wildcard Import
    You can also import all classes from a package using a wildcard (`*`):
    ```java
    import java.util.*;
    ```
    This statement imports all classes in the `java.util` package. Note that it does not import sub-packages.
    #### Static Import
    Static import allows you to import static members of a class so that you can use them without the class name prefix. For example:
    ```java
    import static java.lang.Math.PI;
    import static java.lang.Math.*; // imports all static members of Math class
    ```
    This way, you can use `PI` directly instead of `Math.PI`, and `sqrt()` directly instead of `Math.sqrt()`.
    Example
    Here’s an example that demonstrates the use of packages and import statements:
    1. Directory Structure:
    ```
    src
    └── com
    └── example
    └── myapp
    └── Main.java
    └── utils
    └── Helper.java
    ```
    2. `Helper.java` in `com.example.myapp.utils` package:
    ```java
    package com.example.myapp.utils;
    public class Helper {
    public static void printMessage() {
    System.out.println("Hello from Helper!");
    }
    }
    ```
    3. `Main.java` in `com.example.myapp` package:
    ```java
    package com.example.myapp;
    import com.example.myapp.utils.Helper; // Importing Helper class
    public class Main {
    public static void main(String[] args) {
    Helper.printMessage(); // Using Helper class method
    }
    }
    ```
    Key Points
    - **Package Declaration**: Must be the first statement (except for comments) in a Java file.
    - **Directory Structure**: Should mirror the package structure.
    - **Import Statements**: Should be placed after the package declaration and before any class declarations.
    - **No Circular Dependencies**: Avoid circular dependencies between packages to maintain a clean design.
    By organizing your code with packages and using import statements properly, you can create well-structured, maintainable, and scalable Java applications.
  • ИгрыИгры

Комментарии •