Object Creation and Storage (Instantiation)
// Homework Hack #1: Object Creation Practice
public class ObjectCreation {
public static void main(String[] args) {
// 1. Create two Car objects using 'new'
// Example: Car car1 = new Car("Tesla", 2024);
// 2. Print each car's info
// Example: System.out.println(car1);
}
}
class Car {
// 1. Declare variables: brand, year
// 2. Create a constructor to set those variables
// 3. Add a method or toString() to display car info
}
Homework Hack #2 — Heap vs Stack Storage Demo
Goal - Understand where data is stored in memory — stack vs heap.
Instructions:
- Create a Book class with one variable: String title.
- In main(), create:
- A primitive variable (e.g. int pages = 300;)
- A Book object (e.g. Book b1 = new Book(“Java Basics”);)
- Copy both into new variables (int pagesCopy = pages;, Book b2 = b1;)
Change the original values and print everything — watch what changes.
// Homework Hack #2: Heap vs Stack Storage Demo
public class HeapVsStack {
public static void main(String[] args) {
// 1. Create a primitive variable (int pages)
// 2. Create another primitive variable that copies it
// 3. Create a Book object (Book b1 = new Book("Java Basics");)
// 4. Create another Book reference (Book b2 = b1;)
// 5. Change the original primitive and the Book title
// 6. Print both sets of values to compare behavior
}
}
class Book {
// 1. Declare variable: String title
// 2. Create a constructor to set the title
// 3. Create a toString() to show the title
}