Chapter 2 · Objects

Classes and objects

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A class can describe a thing

So far a class has held main and static methods. A class can also describe a kind of thing, such as a bank account. Your program then makes as many of them as it needs.

In this lesson you will:

  • make objects with new and see where they live in memory,
  • write a class with fields, a constructor and methods,
  • build one class on top of another.
public class Main {
    public static void main(String[] args) {
        Account a = new Account("Ada", 10);
        Account b = new Account("Grace", 20);
        a.deposit(5);
        System.out.println(a.owner + ": " + a.balance);
        System.out.println(b.owner + ": " + b.balance);
    }
}
public class Main {
    public static void main(String[] args) {
        Account a = new Account("Ada", 10);
        a.deposit(5);
        System.out.println(a);
    }
}
public class Main {
    public static void main(String[] args) {
        Account a = new Account("Ada", 10);
        a.balance = -5;
        System.out.println(a);
    }
}
public class Main {
    public static void main(String[] args) {
        Account a = new Account("Ada", 50);
        a.withdraw(30);
        System.out.println(a);
        a.withdraw(100);
        System.out.println(a);
    }
}
public class Main {
    public static void main(String[] args) {
        Checking c = new Checking("Ada", 10);
        c.deposit(5);
        c.chargeFee();
        System.out.println(c);
    }
}
public class Main {
    public static void main(String[] args) {
        Savings s = new Savings("Grace", 200);
        s.addInterest();
        System.out.println(s);
    }
}
public class Main {
    public static void main(String[] args) {
        Rectangle r = new Rectangle(3, 4);
        System.out.println(r + " has area " + r.area());
        r.scale(2);
        System.out.println(r + " has area " + r.area());
        Square s = new Square(5);
        System.out.println(s + " has area " + s.area());
    }
}
public class Main {
    public static void main(String[] args) {
        Account a = new Account("Ada", 50);
        Account b = new Account("Grace", 20);
        a.transfer(b, 30);
        System.out.println(a + ", " + b);
        b.transfer(a, 45);
        System.out.println(a + ", " + b);
        b.transfer(a, 10);
        System.out.println(a + ", " + b);
    }
}
Terminal
Step 1 of 8A class and two objects

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)