Skip to content
Ameen Abozaid
BEG-020

Classes and objects

  • 7 Lessons
  • 80 min of reading
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Fields
  • constructors
  • methods
  • this
  • object creation

Learn first

80 min of reading · 4 h of practice (estimate)

Learning outcomes

  1. Write a class with fields, constructors (including overloaded ones), and instance methods.
  2. Create objects with new and call their methods.
  3. Use this to refer to fields and to chain constructors.
  4. Explain the difference between instance and static members and choose correctly.

Lessons

  1. 01Writing your first class12 min read
  2. 02Constructors11 min read
  3. 03Instance methods and this11 min read
  4. 04Creating and using objects11 min read
  5. 05static: belonging to the class12 min read
  6. 06Demo: Bank Accounts v1 from an empty file11 min read
  7. 07Demo: static vs instance, seen in the debugger12 min read

Lab and exercises

Lab

BEG-020-LAB1 · Bank Accounts v1

Outcomes: 1, 2, 3, 4 · Time: ~2 h Write an Account class with owner, number, and balance; a full constructor and an overloaded one; deposit and withdraw. In main, create several accounts and use them. Add a static counter of accounts created. Acceptance: compiles, works, no logic in main beyond wiring.

Exercises (assessment: 5 exercises plus Bank Accounts v1)

IDOutcomeTask
BEG-020-EX011Write a Book class with title, author, pages, and two constructors (one with pages defaulting to 100).
BEG-020-EX022Create two Book objects, change one's pages, and show the other is unchanged.
BEG-020-EX033Find and fix the bug: Book(String title) { title = title; }.
BEG-020-EX043Rewrite the second constructor of EX01 to use this(...) to call the first.
BEG-020-EX054Decide static or instance for: pages, Book.count, isLong(), MAX_PAGES. Justify each.
Show solutions
  • EX01
    java
    public class Book {
        String title, author; int pages;
        Book(String title, String author, int pages) { this.title = title; this.author = author; this.pages = pages; }
        Book(String title, String author) { this(title, author, 100); }
    }
  • EX02 a.pages = 300; then b.pages still prints its own value; each object has its own copy.
  • EX03 The parameter is assigned to itself and the field stays null. Use this.title = title;.
  • EX04 Shown in EX01: this(title, author, 100); must be the first statement.
  • EX05 pages instance; Book.count static (shared); isLong() instance (uses this book's pages); MAX_PAGES static final constant.
  • Outcomes 1-4 are also covered by the lab and quiz.

Quiz

Outcomes: O1 write a class · O2 create objects · O3 this · O4 static vs instance.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1A constructor:O1
  2. 2If you write any constructor yourself, the free no-argument one:O1
  3. 3Several constructors with different parameters is called:O1
  4. 4The default value of an int field is:O1
  5. 5To create an object:O2
  6. 6After Account b = a; there are:O2
  7. 7Calling a method on a null reference throws:O2
  8. 8In Account(String owner) { owner = owner; } the field:O3
  9. 9this(...) inside a constructor:O3
  10. 10Which should be static?O4

Practical part

Five exercises BEG-020-EX01 to EX05 plus Bank Accounts v1. Pass: at least four exercises correct and the lab meeting its acceptance.

Common mistakes

Everything static; constructors that do heavy work; shadowing fields without this.

Assessment

5 exercises plus Bank Accounts v1.

Career relevance

Every Spring bean (JNR-009) and entity (JNR-028) is a class.

Type to search levels, phases, modules and lessons.