Sub topic 1
Classes as definitions, objects as instances
A class is a definition of a kind of thing in a Java program. It describes what information objects of that kind can hold and what they can do.
An object is a particular thing created from a class. An object is also called an instance of its class. For example, a Dog class can describe dogs in a program. A dog named Milo and a dog named Luna can be two separate Dog objects.
The class is the shared definition. The objects are the individual things made from it. One class can have many instances, or none at all.
Define a class and make objects
This program defines a Dog class and makes two dogs. Save it in a file named Main.java.
class Dog {
String name;
}
public class Main {
public static void main(String[] args) {
Dog firstDog = new Dog();
Dog secondDog = new Dog();
firstDog.name = "Milo";
secondDog.name = "Luna";
System.out.println(firstDog.name);
System.out.println(secondDog.name);
}
}To run it, you need a Java Development Kit (JDK) installed. The JDK includes tools for building and running Java programs. Open a terminal, a program where you type commands, in the folder containing Main.java. Run:
javac Main.java
java MainThe javac command compiles the code: it translates the code into a form Java can run. The java command then starts the program. You should see:
Milo
LunaRead the example
The first part defines the kind of object we want:
class Dog {
String name;
}The word class begins a class definition, and Dog is the class name. Inside it, name is a field: a place where an object can hold a value. String is the Java type used for text. A type tells Java what kind of value is expected.
The program also defines a class named Main. Its main method is where Java starts running this program. A method is a named set of instructions.
Inside main, these lines make two objects:
Dog firstDog = new Dog();
Dog secondDog = new Dog();The word new creates an object. Each line creates a separate Dog object. firstDog and secondDog are variables, or named places for values. Here, each variable stores a way to reach one of the objects.
The next two lines give the objects different names:
firstDog.name = "Milo";
secondDog.name = "Luna";The dot lets the program reach the name field of the chosen object. Each dog has its own name value. The last two lines use System.out.println to print those values.
Keep the distinction clear
| In the example | Role |
|---|---|
| `Dog` | The class definition and the type of both dogs |
| Milo’s dog object | One instance of `Dog` |
| Luna’s dog object | Another instance of `Dog` |
| `firstDog` | A variable used to reach Milo’s dog object |
Both objects follow the same Dog definition, so both have a name field. Their values can still differ. Changing Milo’s name does not change Luna’s name.
Exercise
Change the first dog’s name to "Rex" in the program and run it. What are the two output lines?
Show answer
The output is Rex on the first line and Luna on the second. The two objects have separate name values.
Exercise
If a program uses one Dog class to create three Dog objects, how many Dog instances does it have?
Show answer
Three. Each Dog object is an instance of the Dog class.