0
0
Javaprogramming~15 mins

Import statement usage in Java - Mini Project: Build & Apply

Choose your learning style8 modes available
folder_codeImport statement usage
📖 Scenario: You are creating a simple Java program that uses classes from the java.util package to work with lists.
🎯 Goal: Learn how to use import statements to include classes from other packages and use them in your Java program.
📋 What You'll Learn
Create a Java class named ImportExample
Import the ArrayList class from java.util package
Create an ArrayList of strings with some values
Print the contents of the ArrayList
💡 Why This Matters
🌍 Real World
Import statements let you use useful tools and classes made by others, so you don't have to write everything yourself.
💼 Career
Knowing how to import and use classes is essential for Java programming jobs, as most real projects use many libraries and packages.
Progress0 / 4 steps
1
Create the Java class and main method
Create a public class called ImportExample with a main method inside it.
Java
💡 Need a hint?

Start by writing public class ImportExample and add the main method inside it.

2
Add the import statement for ArrayList
Add an import statement to import java.util.ArrayList at the top of the file, before the class declaration.
Java
💡 Need a hint?

Write import java.util.ArrayList; at the very top of your file.

3
Create an ArrayList of strings
Inside the main method, create an ArrayList<String> called fruits and add these strings to it: "Apple", "Banana", "Cherry".
Java
💡 Need a hint?

Create the list with new ArrayList<>() and add items using add().

4
Print the ArrayList contents
Add a System.out.println statement inside the main method to print the fruits list.
Java
💡 Need a hint?

Use System.out.println(fruits); to print the list.