A First Look at Classes and Objects
Java's way of modeling real things
You have already been using classes since your very first program, since every bit of Java code lives inside one. Now it is time to see what classes can really do.
Think of a class as a blueprint, and an object as something built from that blueprint. A single blueprint for a house can be used to build many actual houses, each with its own address and its own family living inside. Classes and objects work the same way.
public class Dog {
String name;
int age;
void bark() {
System.out.println(name + " says woof!");
}
}
This Dog class describes what every dog object will have: a name, an age, and the ability to bark. Nothing has actually been created yet, this is just the blueprint.
Creating an object from a class
To build an actual dog from the blueprint, you use the new keyword:
Dog myDog = new Dog();
myDog.name = "Rex";
myDog.age = 3;
myDog.bark();
This creates one specific dog object, gives it a name and an age, and then calls its bark method, which would print Rex says woof! to the screen.
You could create a second, completely separate dog object from the exact same class, and it would have its own name and age, independent of the first:
Dog anotherDog = new Dog();
anotherDog.name = "Biscuit";
anotherDog.age = 1;
anotherDog.bark();
Classes and objects are the heart of how Java organizes larger programs, letting you model real world things, like dogs, users, or bank accounts, as clean, reusable blueprints.
Quick check
Q1 What is a class best described as?
Q2 Which keyword is used to create an object from a class?
Want to save your progress? It's free.
Create a free account