AI Hints
Home›Java Tutorial›Classes and Objects, Fields and Constructors
Java · Lesson 64 of 120

Classes and Objects

Your own types.

⏱ 2 min read▶ Video at 1:19:55🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

So far we've used types Java gives us, like int, and String. Now we'll make our own. A class describes a new type of thing, and objects are the actual things.

  • Java's types so far
  • Now your own
  • Classes and objects

Class and object

A class is like a blueprint, or a cookie cutter. It describes what every book has, a title, an author, and pages. An object is one real thing made from that blueprint. You can make as many objects as you like, from one class.

Class

  • The blueprintBookWritten once

Object

  • A real thingDune, Emma, …Made with new

Book.java

Here's our first class, in its own file, Book dot java. It has three variables inside, a title, an author, and pages. Every book object will get its own copy of these. There's no main method here. This file just describes a book.

Main.javajava
public class Book {
    String title;
    String author;
    int pages;
}

Making objects

In Main, new Book creates a new book object. A dot, then a field name, reads or sets that object's value. We make two separate books. B's author was never set, so it's null. Run it. Each book keeps its own values. And b has no author.

Main.javajava
public class Main {
    public static void main(String[] args) {
        Book a = new Book();
        a.title = "Dune";
        a.author = "Frank Herbert";
        a.pages = 412;

        Book b = new Book();
        b.title = "Emma";
        b.pages = 474;

        System.out.println(a.title + " by " + a.author + ", " + a.pages + " pages");
        System.out.println(b.title + ", " + b.pages + " pages");
        System.out.println("Author of b: " + b.author);
    }
}
Output
Dune by Frank Herbert, 412 pages
Emma, 474 pages
Author of b: null
Also uses Book.java
public class Book {
    String title;
    String author;
    int pages;
}

Variables hold references

A variable doesn't hold the object itself. It holds a reference, an arrow pointing to the object. Same equals a copies the arrow, not the book. So changing same changes a too. Two equals signs checks if two arrows point to the same object. Run it. A's title changed. A and same are the same object, but a and other are not.

Main.javajava
public class Main {
    public static void main(String[] args) {
        Book a = new Book();
        a.title = "Dune";
        Book same = a;
        same.title = "Dune Messiah";
        System.out.println(a.title);
        Book other = new Book();
        other.title = "Dune Messiah";
        System.out.println(a == same);
        System.out.println(a == other);
    }
}
Output
Dune Messiah
true
false
Also uses Book.java
public class Book {
    String title;
    String author;
    int pages;
}

Summary

A class is a blueprint, and objects are made from it with new. Each object has its own values. Variables hold references to objects. Next, a closer look at fields.

Key takeaways
  • class = blueprint
  • new makes an object
  • Variables hold references
Next up: Fields — the data inside an object