What an object is and why you need one
An object in Java is a container that holds data and the actions that work on that data. When you write Java code, you are almost always working with objects. A single object might represent a person (with data like name and age, and actions like "have a birthday"), a bank account (with a balance and actions like "withdraw money"), or a car (with properties like color and fuel level, and actions like "accelerate").
Creating an object means taking a template called a class and making a real instance of it that your program can use. The class is the blueprint; the object is the actual thing you build from that blueprint. You need objects because they let you organize your code into pieces that make sense together, and they let you create many similar things without writing the same code over and over.
Key Takeaways
- A class is a template that describes what data and actions an object will have; an object is one actual instance created from that template.
- You create an object by writing the class name, the new keyword, and the class name again with parentheses, like Person person1 = new Person();
- A constructor is a special method inside the class that runs when you create an object and can set up the object's starting data.
- You access an object's data and actions using the dot operator, like person1.name or person1.haveBirthday();
Writing a class before you can create an object
Before you can create an object, you need a class. A class is written in its own file with a name that matches the filename. If you want to create Person objects, you write a file called Person.java that contains the class definition.
Inside the class, you declare what data each object will hold. These are called fields or instance variables. You also write the methods — the actions the object can perform. Here is a simple example:
public class Person { public String name; public int age; public void haveBirthday() { age = age + 1; } }
This class says: a Person has a name (text) and an age (a number). A Person can perform the action haveBirthday(), which increases their age by one. The word public means these fields and methods are visible to other parts of your program.
Creating your first object using the new keyword
Once you have written a class, you create an object from it by using the new keyword. The basic pattern is: write the class name, then a variable name, then an equals sign, then the word new, then the class name again with parentheses.
Person person1 = new Person();
This line creates one Person object and stores it in a variable called person1. The new keyword tells Java to build a new object from the Person class template. After this line runs, person1 is a real object that exists in your program's memory.
You can create as many objects as you need from the same class. Each one is separate:
Person person1 = new Person(); Person person2 = new Person(); Person person3 = new Person();
Now you have three different Person objects. Changes to one do not affect the others.
Setting and reading an object's data
After you create an object, you can set its data and read it back using the dot operator. The dot connects the object's name to the field you want to access.
Person person1 = new Person(); person1.name = "Alice"; person1.age = 30;
These lines create a Person object and then set its name to "Alice" and its age to 30. You can also read the data back:
System.out.println(person1.name); System.out.println(person1.age);
This prints "Alice" and "30" to the screen. The dot operator works the same way whether you are setting data or reading it.
Using constructors to set up an object when it is created
A constructor is a special method that runs automatically when you create an object. It is useful for setting up the object's starting data so you do not have to set each field separately afterward. A constructor has the same name as the class and no return type.
public class Person { public String name; public int age; public Person(String initialName, int initialAge) { name = initialName; age = initialAge; } public void haveBirthday() { age = age + 1; } }
Now when you create a Person, you pass the name and age inside the parentheses:
Person person1 = new Person("Alice", 30);
The constructor runs immediately, and person1 is created with name set to "Alice" and age set to 30. This is cleaner than creating the object and then setting each field separately. You can write multiple constructors with different numbers of parameters if you want different ways to create an object.
Calling methods on an object
Methods are the actions an object can perform. You call a method using the dot operator, just like you access fields, but you add parentheses at the end.
Person person1 = new Person("Alice", 30); person1.haveBirthday(); System.out.println(person1.age);
This creates a Person, calls the haveBirthday() method, and then prints the age. The age will be 31 because the method increased it by one. Methods can take parameters (data you pass in) and can return a value back to you. For example, a method might calculate something and return the result:
public int yearsUntilRetirement() { return 65 - age; }
You would call this method and capture the result like this:
int yearsLeft = person1.yearsUntilRetirement();
Common mistakes when creating objects
The most common mistake is forgetting the new keyword. If you write Person person1 = Person(); without new, Java will not understand what you mean and will show an error. The new keyword is required every time you create an object.
Another mistake is trying to use an object before you have created it. If you write code that uses person1 but you never wrote Person person1 = new Person(); first, your program will crash. Always create the object before you try to use it.
A third mistake is confusing the class name with the object name. The class is the template (like "Person"), and the object is the variable you create (like "person1"). You can have many objects from the same class, and each one is independent.
Frequently Asked Questions
What is the difference between a class and an object?
A class is a template or blueprint that describes what data and methods an object will have. An object is one actual instance created from that class. Think of a class as a recipe and an object as a cake made from that recipe. You write the class once, but you can create many objects from it.
Do I have to write a constructor?
No. If you do not write a constructor, Java provides a default one that takes no parameters and does nothing. You can create objects without a constructor, but you will have to set the fields manually afterward. Writing a constructor is optional but often makes your code cleaner.
Can I create an object without using the new keyword?
No. The new keyword is required to create an object in Java. It tells Java to allocate memory for the object and call the constructor. Without new, you do not have an actual object, just a variable that points to nothing.
What happens if I create two objects from the same class?
Each object is completely separate. If you change data in one object, it does not affect the other. They are two different instances in memory, even though they were created from the same class template.
Can an object call its own methods?
Yes. Inside a method, you can call another method on the same object using this.methodName() or just methodName(). For example, a method could call another method to do part of its work.