Classes and objects

A class is a type that a program defines: it holds fields, which are the data of an object, and methods, which are what the object can do. An object is one value of that type, created with new, and every object has its own copy of every instance field.

A class

The class below has two fields, a constructor that fills them and a main that creates two objects. The fields have no private yet, which the encapsulation lesson fixes; here they keep the example short.

The file Book.java:

public class Book { String title; int pages; Book(String title, int pages) { this.title = title; this.pages = pages; } public static void main(String[] args) { Book dune = new Book("Dune", 412); Book emma = new Book("Emma", 474); System.out.println(dune.title + ": " + dune.pages); System.out.println(emma.title + ": " + emma.pages); dune.pages = 500; System.out.println("dune: " + dune.pages + ", emma: " + emma.pages); } }

javac -Xlint:all Book.java
java Book

Dune: 412
Emma: 474
dune: 500, emma: 474

The constructor is the method with the name of the class and no return type. new reserves the object, runs the constructor and gives back a reference. this.title is the field and title alone would be the parameter, because the parameter hides the field inside the constructor.

Fields belong to objects

Changing dune.pages changed one object and left the other at 474, because each new created its own fields. The class is the type, the objects are the values, and the fields live in the values.

A field declared with static would be the exception: one copy for the whole class, shared by every object. That is lesson 037, and the distinction between an instance field and a static one is one of the two or three ideas that make the rest of the object part easy.

new and the constructor

new Book("Dune", 412) runs the constructor with the two arguments. The arguments are copied into the parameters, the body assigns them to the fields, and the new object is returned. A class with no constructor gets a default one with no parameters, which is why new Book() compiles until the first constructor is written.

References

A variable of a class type holds a reference, not the object. Copying the variable copies the reference, and both names then point at one object; two objects created with the same arguments are still two objects.

public class Reference { public static void main(String[] args) { Book a = new Book("Dune", 412); Book b = a; b.pages = 500; System.out.println("a.pages: " + a.pages); System.out.println("a == b: " + (a == b)); Book c = new Book("Dune", 500); System.out.println("a == c: " + (a == c)); } } class Book { String title; int pages; Book(String title, int pages) { this.title = title; this.pages = pages; } }

javac -Xlint:all Reference.java
java Reference

a.pages: 500
a == b: true
a == c: false

The first line proves that a and b are one object: writing through b is visible through a. a == c is false because c is another object with the same contents, and a.equals(c) would be false too until the class overrides equals, which is lesson 040.

Exercises

  1. Add a year field to Book.java and pass it to the constructor.
  2. Create a third book in Book.java and print all three titles with one loop over an array of books.
  3. In Reference.java, set b = new Book("Emma", 474) before changing the pages. Do the two names still share an object?
  4. Remove the constructor from Book.java and try new Book(). What does the compiler give you instead?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does new return?

    Answer

    A reference to the new object

    Every value of a class type is a reference; new creates the object and returns the reference.

  2. What does this.title mean inside a constructor?

    Answer

    The field of the object being constructed

    this is the current object, so this.title is its field; title alone would be the parameter.

  3. What does a second object created with the same arguments share with the first?

    Answer

    Nothing: it has its own fields

    Each new creates an object with its own instance fields, even when the values are equal.

  4. What does this program print? public class Q1 { public static void main(String[] args) { Box a = new Box(1); Box b = new Box(2); System.out.println(a.value + " " + b.value); } } class Box { int value; Box(int value) { this.value = value; } }

    Answer

    1 2

    Each constructor stores its own argument in its own object.

  5. What does this program print? public class Q2 { public static void main(String[] args) { Box a = new Box(1); Box b = a; b.value = 9; System.out.println(a.value); } } class Box { int value; Box(int value) { this.value = value; } }

    Answer

    9

    The assignment copies the reference, so a and b name one object.

  6. What does this program print? public class Q3 { public static void main(String[] args) { Box a = new Box(1); Box b = new Box(1); System.out.println(a == b); } } class Box { int value; Box(int value) { this.value = value; } }

    Answer

    false

    Two new expressions create two objects, so the references differ.

  7. What is the name of a constructor?

    Answer

    The name of the class

    A constructor is the method whose name is the class name and which has no return type.

  8. What does this program print? public class Q4 { public static void main(String[] args) { Box box = new Box(); System.out.println(box.value); } } class Box { int value = 7; }

    Answer

    7

    The class has no constructor, so the default one runs and the field initialiser sets 7.

  9. What is a field of a class?

    Answer

    A variable that belongs to each object

    A field is declared in the class body and stored in every object created from it.

  10. What does this program print? public class Q5 { public static void main(String[] args) { Box a = new Box(3); Box b = new Box(4); a = b; System.out.println(a.value + " " + b.value); } } class Box { int value; Box(int value) { this.value = value; } }

    Answer

    4 4

    After a = b both names hold the same reference, so both print the value of the second object.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev