But does not contain package

In Java, if you want to import specific classes from a package without importing the entire package, you can use the “import” statement followed by the package name, a dot, and the class name. By doing so, you can access the classes directly without needing to specify the package every time.

Here’s an example:

    
// Importing a specific class from a package
import java.util.ArrayList;

public class MyClass {
  public static void main(String[] args) {
    // You can now directly use the ArrayList class without specifying the package
    ArrayList myList = new ArrayList<>();

    // Perform operations using the ArrayList
    myList.add("Element 1");
    myList.add("Element 2");

    // Print the elements in the list
    for (String element : myList) {
      System.out.println(element);
    }
  }
}
    
  

In the example above, we import only the “ArrayList” class from the “java.util” package. This allows us to use the ArrayList class without needing to write “java.util.ArrayList” every time. We create an instance of ArrayList, add elements to it, and then print the elements using a for-each loop.

Read more interesting post

Leave a comment