0
0
Javaprogramming~15 mins

Instance variables in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Instance variables
πŸ“– Scenario: Imagine you are creating a simple program to keep track of a book's details in a library system. Each book has a title and an author.
🎯 Goal: You will create a class called Book with instance variables to store the title and author. Then, you will create an object of this class and print its details.
πŸ“‹ What You'll Learn
Create a class called Book
Add instance variables title and author of type String
Create an object of Book with title "The Alchemist" and author "Paulo Coelho"
Print the book's title and author using the object
πŸ’‘ Why This Matters
🌍 Real World
Instance variables are used to store information about objects in real-world programs, like details of books, students, or products.
πŸ’Ό Career
Understanding instance variables is essential for object-oriented programming, which is widely used in software development jobs.
Progress0 / 4 steps
1
Create the Book class with instance variables
Create a class called Book with two instance variables: String title and String author.
Java
Need a hint?

Instance variables are declared inside the class but outside any method.

2
Create a Book object with specific title and author
In the Main class, create a Book object called myBook. Set its title to "The Alchemist" and author to "Paulo Coelho".
Java
Need a hint?

Create the object using new Book() and assign values to instance variables using the dot . operator.

3
Print the book's title and author
Use System.out.println to print the book's title and author from the myBook object. Print the title first, then the author on the next line.
Java
Need a hint?

Use System.out.println twice, once for the title and once for the author.

4
Run the program to see the output
Run the program to display the book's title and author on the screen.
Java
Need a hint?

The program should print the title and author on separate lines.