0
0
Javaprogramming~15 mins

Creating packages in Java - Try It Yourself

Choose your learning style8 modes available
folder_codeCreating packages
📖 Scenario: You are organizing a small Java project for a bookstore. You want to keep your code neat by using packages.
🎯 Goal: Create a package named bookstore and place a class Book inside it. Then create another class Store in the default package that uses the Book class.
📋 What You'll Learn
Create a package named bookstore
Create a class Book inside the bookstore package
Add a String field title and a constructor to Book
Create a class Store in the default package
In Store, create a main method that creates a Book object
Print the book's title in the main method
💡 Why This Matters
🌍 Real World
Packages help keep Java projects organized, especially when many classes are involved. This is common in real software projects.
💼 Career
Understanding packages is essential for Java developers to write clean, maintainable, and scalable code.
Progress0 / 4 steps
1
Create the Book class inside the bookstore package
Create a package named bookstore. Inside it, create a public class called Book with a public String field named title and a constructor that takes a String parameter called title and assigns it to the field.
Java
💡 Need a hint?

Start with package bookstore; at the top. Then define the class and constructor.

2
Create the Store class with a main method
Create a public class called Store in the default package. Inside it, create a public static void main(String[] args) method.
Java
💡 Need a hint?

Define the Store class and add the main method inside it.

3
Import the Book class and create a Book object
In the Store class, add an import statement for bookstore.Book. Inside the main method, create a Book object named myBook with the title "Java Basics".
Java
💡 Need a hint?

Use import bookstore.Book; at the top of Store. Then create the object inside main.

4
Print the book's title
In the main method of Store, add a line to print the title of the myBook object using System.out.println.
Java
💡 Need a hint?

Use System.out.println(myBook.title); to print the title.