Classes and instance fields
What you will learn
A class defines a type with data and behavior. An object is a particular instance of that class. Instance fields hold state belonging to one object, and instance methods read or change that state.
class Book { int pages; void addPages(int amount) { pages += amount; }}public class Main { public static void main(String[] args) { Book first = new Book(); Book second = new Book(); first.addPages(20); second.addPages(5); System.out.println(first.pages); System.out.println(second.pages); }}Expected output:
205Each new expression creates a separate object. The calls act on different receivers.
How it works
A class declaration starts with class and a name, followed by its body. In the first example, Book is a second top-level class in Main.java; only Main is public. A field such as int pages; belongs to every Book instance separately. Book first = new Book(); declares a reference variable and creates an object. The variable stores a reference, not a copy of the object. The dot selects a field or invokes a method on the referenced object. first.addPages(20) changes only the object that first refers to. The simple package-access fields in the first two examples are for learning; production classes often use private fields and meaningful operations instead. A private field cannot be directly accessed from an unrelated class. A method such as pages() can expose the necessary value without allowing arbitrary replacement. Uninitialized instance fields have defaults (0 for int, false for boolean, null for references), unlike unassigned local variables. Do not rely on null as a valid domain value merely because it is a default.
class Label { int number; boolean printed; String text;}public class Main { public static void main(String[] args) { Label label = new Label(); System.out.println(label.number); System.out.println(label.printed); System.out.println(label.text); }}Expected output:
0falsenullDefault field values exist before explicit initialization. A local variable without an assignment cannot be read in the same way.
Check an edge case
class Shelf { private int count; void addOne() { count++; } int count() { return count; }}public class Main { public static void main(String[] args) { Shelf shelf = new Shelf(); shelf.addOne(); shelf.addOne(); System.out.println(shelf.count()); }}Expected output:
2Main asks the shelf to add an item; it does not edit the private field directly.
Common mistakes
Assigning Book second = first; does not create another Book. Both variables refer to the same object. Use a second new Book() when independent state is required. Also avoid accessing a private field directly from Main; call the object’s public or accessible operation instead.
Practice
Create a Shelf class with a private int count initialized to zero. Add void addOne() and int count() methods. Create two shelves, add items to one and verify that the other stays empty. No negative inputs are needed.
Save each complete example as Main.java in a fresh directory, compile with javac --release 21 Main.java, then run java Main. Each example is independent. A local JDK is required; the ByteMind Java runner is not verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Classes and instance fields quiz
Check understanding using Java 21-compatible examples and the stated assumptions.
Which expression creates a second Book object?
What is the initial value of an int instance field with no initializer?
If a and b are separately constructed shelves, what happens when a.addOne() runs?
How should Main read a private count from an unrelated Shelf class?
What does a Book reference variable hold?