Sub topic 11
Classes and Objects
So far, you have used built-in types like int and String. In this chapter you will learn how to create your own types using classes. A class is a blueprint that describes a kind of thing. An object is one actual thing built from that blueprint.
Think of a class like a cookie cutter and objects like the cookies. One cutter can make many cookies, and each cookie is a real, separate thing.
Why Classes Matter
Classes let you group related data and the actions that go with it. Instead of keeping a dog's name in one variable and its behavior somewhere else, you put them together in one place. This keeps programs organized as they grow.
Defining a Class
Here is a simple class:
class Dog {
String name; // a field: data each dog holds
void speak() { // a method: something each dog can do
System.out.println(name + " says Woof!");
}
}- Field: a variable that belongs to each object, like
name. - Method: a function that belongs to the class, like
speak().
This code only describes dogs. To get an actual dog, you must create an object from the class.
Creating Objects
The new keyword creates an object. This is called instantiation — making one instance of the class.
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // create a Dog object
d.name = "Rex"; // set its field
d.speak(); // call its method
}
}Output:
Rex says Woof!The part before the dot, d, is the object. The part after the dot is what you want from it. You can make many dogs, each with its own name:
Dog a = new Dog();
a.name = "Rex";
Dog b = new Dog();
b.name = "Bella";
a.speak(); // Rex says Woof!
b.speak(); // Bella says Woof!Each object has its own copy of the fields. Changing a.name does not change b.name.
The Dot Operator
The dot (.) means "reach inside." You use it to read or set a field, or to run a method:
System.out.println(d.name); // read a field
d.speak(); // call a methodThe `this` Keyword
Inside a method, the word this means "this very object." It matters when a parameter has the same name as a field:
class Cat {
String name;
void rename(String name) {
this.name = name; // this.name is the field, name is the parameter
}
}this.name points to the object's field. Plain name points to the method's parameter. You will use this more in the next chapter on constructors.
Objects as References
A class variable like Dog d does not hold the object itself. It holds a reference — a value that points to where the object lives in memory.
Dog a = new Dog();
a.name = "Rex";
Dog b = a; // b points to the SAME object
b.name = "Bella";
System.out.println(a.name); // Bella!There is only one dog, and both a and b point to it. Renaming it through b changes what a sees too. This is a common source of confusion, so watch for it.
A Complete Small Program
class Book {
String title;
int pages;
void describe() {
System.out.println(title + " has " + pages + " pages.");
}
}
public class Main {
public static void main(String[] args) {
Book book1 = new Book();
book1.title = "Java Basics";
book1.pages = 120;
book1.describe();
}
}Output:
Java Basics has 120 pages.Note: put each class in its own file named after the class (Book.java, Main.java), or keep small helper classes in the same file as Main when practicing.
Exercise
What does the new keyword do?
Show answer
It creates a new object from a class in memory, and returns a reference to it that you can store in a variable.
Exercise
If you write Dog x = y; where y is an existing Dog object, how many Dog objects exist?
Show answer
Only one. Both variables point to the same object; x just holds a copy of the reference.