Objects are the building blocks of Java programs
An object in Java is a container that holds data and the actions that work with that data. To create one, you write a class first — a template that describes what the object will look like — then you instantiate it, which means you tell Java to build an actual copy of that template in memory. The simplest way is to use the new keyword followed by the class name and parentheses.
Every object you create comes from a class. The class is the blueprint; the object is the thing you build from it. If a class is a recipe for a cake, an object is the actual cake sitting on your counter. You can make many cakes from one recipe, and you can make many objects from one class.
Key Takeaways
- A class defines what an object will contain and do; an object is an actual instance of that class created with the new keyword.
- The simplest object creation looks like MyClass myObject = new MyClass(); where MyClass is the class name and myObject is the variable that holds the object.
- Constructors are special methods inside a class that run when you create an object and can accept information you pass in parentheses.
- Objects created from the same class can hold different data because each one is a separate copy in memory.
- You access an object's data and methods using the dot operator: myObject.methodName() or myObject.variableName.
Writing a class before you create an object
Before you can create an object, you need a class. A class lives in its own file with a name that matches the class name. If your class is called Car, the file is Car.java.
Inside the class, you declare variables (also called fields or properties) that hold data, and methods (also called functions) that perform actions. Here is a straightforward example:
public class Car { public String color; public String brand; public int year; public void drive() { System.out.println("The car is driving"); } }
This class describes a car with three pieces of data (color, brand, year) and one action (drive). Now you can create objects from this class.
Using the new keyword to create an object
To create an object, write the class name, give the object a variable name, use the new keyword, and then call the class name again with parentheses:
Car myCar = new Car();
This line does several things at once. It declares a variable called myCar, it tells Java to create a new object from the Car class, and it stores that object in the variable. Now myCar is an actual object you can work with.
You can create as many objects as you want from the same class, and each one is separate:
Car myCar = new Car(); Car yourCar = new Car(); Car theirCar = new Car();
Each of these three objects is independent. If you change the color of myCar, it does not affect yourCar or theirCar.
Constructors let you set up an object when you create it
A constructor is a special method inside a class that runs automatically when you create an object. It has the same name as the class and no return type. Constructors let you pass in information and set up the object's data right away, instead of setting each piece separately afterward.
Here is the Car class with a constructor:
public class Car { public String color; public String brand; public int year; public Car(String c, String b, int y) { color = c; brand = b; year = y; } public void drive() { System.out.println("The car is driving"); } }
Now when you create an object, you pass in the data inside the parentheses:
Car myCar = new Car("red", "Toyota", 2020);
The constructor receives "red", "Toyota", and 2020, and assigns them to the object's variables when ready. This is cleaner than creating an empty object and then setting each variable one at a time.
Accessing an object's data and methods
Once you have created an object, you use the dot operator (a period) to access its variables and call its methods. The syntax is objectName.variableName or objectName.methodName().
If you created a car object like this:
Car myCar = new Car("blue", "Honda", 2022);
You can read its data:
System.out.println(myCar.color); System.out.println(myCar.brand); System.out.println(myCar.year);
You can change its data:
myCar.color = "green";
And you can call its methods:
myCar.drive();
This would print "The car is driving" to the screen. Each object keeps its own copy of the data, so changing myCar.color does not change the color of any other car object.
Objects without a constructor still work
If you do not write a constructor, Java provides a default one automatically. It takes no arguments and does nothing except create the object. This is why the first example worked even though no constructor was written:
Car myCar = new Car();
Java called the invisible default constructor, created the object, and stored it in myCar. The object's variables start with default values (numbers become 0, text becomes empty, true/false values become false). You can then set the variables yourself using the dot operator.
Most real programs write their own constructors because it is clearer and safer to set up an object's data when you create it, rather than leaving it partially empty and hoping the programmer remembers to fill it in later.
Frequently Asked Questions
What is the difference between a class and an object?
A class is a template or blueprint that describes what an object will look like. An object is an actual instance of that class created in memory. You write the class once, but you can create many objects from it. Think of a class as a cookie cutter and objects as the actual cookies.
Can I create an object without using the new keyword?
In most cases, no. The new keyword is the standard way to create objects in Java. There are advanced techniques like reflection or deserialization that can create objects differently, but beginners should always use new.
Do all objects from the same class have to have the same data?
No. Each object is independent. Two car objects can have different colors, brands, and years. The class defines what kinds of data an object can hold, but each object stores its own copy of that data.
What happens if I create an object but never use it?
Java keeps it in memory until the program ends or until the garbage collector decides it is no longer needed. If you create many objects and forget about them, your program can run out of memory. It is good practice to only create objects you actually need.
Can a class have more than one constructor?
Yes. A class can have multiple constructors as long as they take different numbers or types of arguments. This lets you create objects in different ways depending on what data you have available.