A Java object starts with a class definition and the new keyword

Creating a Java object means writing code that builds an instance of a class — a working copy of the blueprint you've defined. The process has two parts: you write a class (the template), then you use the new keyword to create an actual object from that template. Without the new keyword, you have only instructions; with it, you have something your program can use.

The simplest path is to define a class with a constructor, then call that constructor with new. A constructor is a method that runs when the object is created. If you don't write one, Java provides a default constructor that does nothing — but you can write your own to set up the object the way you need it.

Key Takeaways

  • A class is a template; an object is a working copy created with the new keyword followed by the class name and parentheses.
  • A constructor is a method that runs when you create an object and can set up initial values for the object's properties.
  • Instance variables (properties) belong to each object individually, so two objects from the same class can have different values.
  • The new keyword allocates memory for the object and calls the constructor, returning a reference you store in a variable.

Writing a class and its constructor

Start by defining a class. Open a text editor or IDE (like Eclipse or IntelliJ), create a file named after your class, and write the class structure. Here's a straightforward example:

public class Car {   String color;   String model;   int year;   public Car(String color, String model, int year) {     this.color = color;     this.model = model;     this.year = year;   } }

The three lines at the top (String color, String model, int year) are instance variables — they hold data that belongs to each object. The method named Car is the constructor. It has the same name as the class and no return type. When you create a Car object, this constructor runs and fills in the instance variables with the values you provide.

The keyword this means "this object's variable" — it tells Java to store the color parameter into this specific object's color variable, not into some other place. Without this, Java would be confused about which variable you meant.

Creating an object with the new keyword

Once you have a class, create an object by writing the class name, the new keyword, and the constructor call. In another class (or in a main method), write:

Car myCar = new Car("blue", "Honda Civic", 2020);

This line does four things: it declares a variable named myCar, it uses new to allocate memory for a Car object, it calls the Car constructor with the three values you provided, and it stores the reference to that object in myCar. Now myCar holds a working Car object with color set to "blue", model set to "Honda Civic", and year set to 2020.

You can create as many objects as you need from the same class. Each one is separate:

Car myCar = new Car("blue", "Honda Civic", 2020); Car yourCar = new Car("red", "Toyota Camry", 2019); Car theirCar = new Car("black", "Ford F-150", 2021);

All three are Car objects, but each has its own color, model, and year. Changing myCar's color does not change yourCar's color.

Using the object's data and methods

Once you have created an object, you can read its variables and call its methods using the dot operator. If your Car class has a method that prints information:

public class Car {   String color;   String model;   int year;   public Car(String color, String model, int year) {     this.color = color;     this.model = model;     this.year = year;   }   public void displayInfo() {     System.out.println(color + " " + model + " " + year);   } }

You can call that method on your object:

myCar.displayInfo();

This prints "blue Honda Civic 2020" to the console. You can also read the variables directly:

System.out.println(myCar.color);

This prints "blue". The dot connects the object to its data and methods.

What happens when you use new

The new keyword does the actual work of creating the object. When Java sees new Car(...), it reserves a block of memory for the object, initializes all instance variables to their default values (0 for numbers, null for text), then calls the constructor you wrote. The constructor fills in the real values. Finally, new returns a reference — a memory address — which you store in your variable.

A reference is not the object itself; it's a pointer to where the object lives in memory. When you write Car myCar = new Car(...), myCar holds that reference. If you later write Car yourCar = myCar, you're copying the reference, not the object. Both variables now point to the same object in memory. Changing myCar.color also changes yourCar.color because they're the same object.

Constructors with no parameters

You don't have to pass values to a constructor. You can write a constructor that takes no parameters:

public class Car {   String color;   String model;   int year;   public Car() {     this.color = "white";     this.model = "unknown";     this.year = 0;   } }

Now you can create a Car with no arguments:

Car myCar = new Car();

The object is created with default values. You can have multiple constructors in the same class — one that takes no parameters, one that takes three, one that takes two. Java picks the right one based on what you pass to new. This is called constructor overloading.

Common mistakes when creating objects

Forgetting the new keyword is the most common error. Writing Car myCar; creates a variable but does not create an object — myCar is null (empty). You must write Car myCar = new Car(); to actually build the object. If you try to use myCar before you call new, Java throws a NullPointerException.

Another mistake is passing the wrong number or type of arguments to the constructor. If your constructor expects three parameters but you pass two, Java will not compile. The error message tells you what it expected, so read it carefully.

A third mistake is confusing the class with the object. The class is the blueprint; the object is the thing you build from it. You write the class once, but you can create many objects from it. Each object is independent.

Frequently Asked Questions

Do I have to write a constructor?

No. If you don't write one, Java provides a default constructor with no parameters that does nothing. But writing your own constructor lets you set up the object's initial values automatically, which is usually what you want.

What's the difference between a class and an object?

A class is a template or blueprint. An object is a working copy created from that template. You write the class once in your code. You create objects from it using new, and you can create as many as you need. Each object is separate and can have different values.

Can I create an object without using new?

In most cases, no. The new keyword is the standard way to create objects. Some advanced techniques like reflection or cloning exist, but they're rarely needed for basic programming.

What happens to an object when I'm done using it?

Java has automatic garbage collection. When no variable points to an object anymore, Java automatically frees the memory. You don't have to delete objects manually like you would in some other languages.

Can two variables point to the same object?

Yes. If you write Car yourCar = myCar, both variables point to the same object in memory. Changes to one affect the other because they're the same thing. If you want a separate copy, you have to create a new object with new.