Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Object creation
π Scenario: You are creating a simple program to manage a book in a library. Each book has a title and an author.
π― Goal: Build a Java program that creates a Book object with a title and author, then prints the book details.
π What You'll Learn
Create a class named Book with two fields: title and author
Create a Book object with the exact title "The Alchemist" and author "Paulo Coelho"
Print the book details in the format: Title: The Alchemist, Author: Paulo Coelho
π‘ Why This Matters
π Real World
Creating objects is the foundation of Java programming. It helps model real things like books, cars, or people in software.
πΌ Career
Understanding object creation is essential for any Java developer job, as it is the basis for building applications and managing data.
Progress0 / 4 steps
1
Create the Book class with fields
Create a class called Book with two public string fields: title and author.
Java
Hint
Use public String title; and public String author; inside the class.
2
Create a Book object with given title and author
Create a Book object named myBook. Set its title to "The Alchemist" and author to "Paulo Coelho".
Java
Hint
Create the object with new Book() and assign values to myBook.title and myBook.author.
3
Add code to print the book details
Inside the main method, add a System.out.println statement to print the book details in this exact format: Title: The Alchemist, Author: Paulo Coelho using myBook.title and myBook.author.
Java
Hint
Use System.out.println with string concatenation to show the title and author.
4
Run the program to display the book details
Run the program and ensure it prints exactly: Title: The Alchemist, Author: Paulo Coelho
Java
Hint
Make sure your print statement matches the exact output format.
Practice
(1/5)
1. Which of the following is the correct way to create an object of class Car in Java?
easy
A. Car myCar = new Car();
B. Car myCar = Car();
C. new Car myCar();
D. Car myCar = new car();
Solution
Step 1: Understand object creation syntax
In Java, objects are created using the new keyword followed by the class name and parentheses.
Step 2: Check each option
Car myCar = new Car(); uses new Car() correctly with proper capitalization and assignment. Others have syntax errors or wrong capitalization.
Final Answer:
Car myCar = new Car(); -> Option A
Quick Check:
Use new ClassName() to create objects [OK]
Hint: Remember: new + ClassName() creates an object [OK]
Common Mistakes:
Omitting the new keyword
Using wrong capitalization for class name
Missing parentheses after class name
2. Which of the following lines will cause a syntax error when creating an object of class Book?
easy
A. Book b=new Book();
B. Book b = new Book();
C. Book b = new Book;
D. Book b = new Book( );
Solution
Step 1: Recall syntax for object creation
In Java, when creating an object, parentheses must follow the class name even if the constructor has no parameters.
Step 2: Identify the incorrect option
Book b = new Book; misses the parentheses after new Book, causing a syntax error. Others are correct.
Final Answer:
Book b = new Book; -> Option C
Quick Check:
Always use parentheses after class name in new [OK]
Hint: Always include () after class name when using new [OK]
Common Mistakes:
Forgetting parentheses after class name
Confusing object creation with method calls
Using semicolon inside parentheses
3. What will be the output of the following code?
class Dog {
String name;
Dog(String n) {
name = n;
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog("Buddy");
System.out.println(d.name);
}
}
medium
A. Buddy
B. null
C. Dog@someHashCode
D. Compilation error
Solution
Step 1: Understand constructor assignment
The constructor sets the name field to the string passed, which is "Buddy".
Step 2: Check output of print statement
Printing d.name outputs the string "Buddy" stored in the object.
Final Answer:
Buddy -> Option A
Quick Check:
Constructor sets field, printing field shows assigned value [OK]
Hint: Constructor sets values; print field to see stored data [OK]
Common Mistakes:
Expecting default null instead of assigned value
Confusing object reference print with field print
Missing constructor parameters
4. Identify the error in the following code snippet:
class Person {
String name;
Person(String n) {
name = n;
}
}
public class Test {
public static void main(String[] args) {
Person p = Person("Alice");
System.out.println(p.name);
}
}
medium
A. Missing semicolon after System.out.println
B. Constructor name does not match class name
C. Variable p is not declared
D. Missing new keyword when creating object
Solution
Step 1: Check object creation syntax
The code tries to create an object with Person("Alice") but misses the new keyword.
Step 2: Confirm other parts are correct
Constructor name matches class name, variable is declared, and semicolon is present.
Final Answer:
Missing new keyword when creating object -> Option D
Quick Check:
Use new keyword to create objects [OK]
Hint: Always use new before class name to create objects [OK]
Common Mistakes:
Omitting new keyword
Confusing method call with object creation
Incorrect constructor naming
5. Given the class below, which code correctly creates two Student objects with names "John" and "Jane" and prints their names?
class Student {
String name;
Student(String n) {
name = n;
}
}
hard
A. Student s1 = new Student("John");
Student s2 = new Student();
System.out.println(s1.name + ", " + s2.name);
B. Student s1 = new Student("John");
Student s2 = new Student("Jane");
System.out.println(s1.name + ", " + s2.name);
C. Student s1 = new Student();
Student s2 = new Student();
System.out.println(s1.name + ", " + s2.name);
The constructor requires a String parameter. Student s1 = new Student("John");
Student s2 = new Student("Jane");
System.out.println(s1.name + ", " + s2.name); correctly passes names "John" and "Jane".
Step 2: Verify object creation and printing
Student s1 = new Student("John");
Student s2 = new Student("Jane");
System.out.println(s1.name + ", " + s2.name); creates both objects properly and prints their names separated by a comma.
Final Answer:
Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name); -> Option B
Quick Check:
Use new with constructor parameters to create objects [OK]
Hint: Pass required parameters in new ClassName(params) [OK]