Java Creating Objects

In the last lesson you learned about Java creating classes. A blueprint on its own does nothing. You build a real thing from it, and that real thing is an object.

πŸ€” Why we need to create objects

A class is like a cookie cutter. You cannot eat the cutter. It only describes a cookie. To get something real, you press it into dough. A class works the same way. Here is a small Student class.

class Student {
String name;
int age;
}

Right now no student exists.

  • The class is only a plan: β€œa Student has a name and an age”.
  • No name or age is stored anywhere yet.
  • To get a real student with real values, you build one from the plan.
  • Building a real thing from a class is called creating an object.

πŸ†• Creating an object with new

To build an object, use the new keyword, the class name, and round brackets.

  • An object is also called an instance. The two words mean the same thing.
  • This program builds one Student object and stores its name and age.
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // βœ… build a Student object
s1.name = "Alex"; // set its name field
s1.age = 20; // set its age field
System.out.println(s1.name + " is " + s1.age + " years old.");
}
}
class Student {
String name;
int age;
}

Line by line:

  • new Student() builds one real Student object in memory.
  • Student s1 = ... makes a variable s1 that refers to that object.
  • s1.name = "Alex" and s1.age = 20 reach in and store values.
  • s1.name and s1.age later read those values back.
  • Now s1 is a real student, with name and age bundled as one thing.

Output

Alex is 20 years old.

That first line has two halves. Student s1 declares a variable that can refer to a Student. new Student() builds the actual object. The = joins them, so s1 points at the object.

🧭 The reference variable points to the heap

Many people get this wrong, so slow down here.

  • s1 does not hold the object itself. It holds a reference to it.
  • The object lives in memory called the heap. s1 is a pointer to it.
  • Think of a house and its address: the object is the house, s1 is paper with the address.
  • s1.name reads the address, walks to the house, and changes the name there.

This explains a result that surprises beginners: two variables can point at the same object.

public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Alex";
Student s2 = s1; // βœ… copies the address, NOT the object
s2.name = "Riya"; // changes the one shared object
System.out.println(s1.name); // prints Riya, not Alex!
}
}
class Student {
String name;
int age;
}

Why s1.name prints Riya:

  • Student s2 = s1 has no new, so no new object is built.
  • It only copies the address. Both variables now hold the same address.
  • They point at one single object. Change it through s2, see it through s1.

Output

Riya

Remember: new creates a new object. A plain = between references only copies the address.

πŸ”˜ Reaching inside with the dot operator

The dot reaches into an object. It means β€œthe thing belonging to this object”.

  • s1.name reads as β€œthe name of s1”.
  • s1.age reads as β€œthe age of s1”.
  • The dot works for methods too, not just fields.

Let’s give the class a method and call it.

public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Alex";
s1.age = 20;
s1.greet(); // βœ… call a method on the object
}
}
class Student {
String name;
int age;
void greet() {
System.out.println("Hi, I am " + name + " and I am " + age + ".");
}
}

Line by line:

  • s1.name and s1.age use the dot to read or set fields.
  • s1.greet() uses the dot to call a method. The brackets mark it as a method, not a field.
  • Inside greet, the bare word name means β€œthe name of the object it was called on”, which is s1.

Output

Hi, I am Alex and I am 20.

The dot is your one tool for everything inside an object. Fields, prints, methods. Always the same dot.

πŸ‘₯ Many independent objects from one class

Now the real power.

  • Each new builds a brand new object on the heap.
  • Each object gets its own copy of the fields.
  • So changing one object never touches another.

This program builds two separate students from the same class.

public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // first object
s1.name = "Alex";
s1.age = 20;
Student s2 = new Student(); // βœ… a second, separate object
s2.name = "Riya";
s2.age = 22;
s1.age = 21; // change ONLY s1
System.out.println(s1.name + ", " + s1.age);
System.out.println(s2.name + ", " + s2.age);
}
}
class Student {
String name;
int age;
}

The important parts:

  • new Student() twice means two real objects on the heap.
  • s1 and s2 hold different addresses, because each new makes a fresh object.
  • Each object carries its own name and age.
  • We changed s1.age to 21, but s2.age stays 22, completely untouched.

Output

Alex, 21
Riya, 22

This is the whole idea: one blueprint, many independent objects. They share the shape from the class. They do not share the values.

Here is a Car class with fields and a method.

public class Main {
public static void main(String[] args) {
Car car1 = new Car();
car1.brand = "Toyota";
car1.speed = 0;
Car car2 = new Car();
car2.brand = "Honda";
car2.speed = 0;
car1.accelerate(30); // speed up only car1
car2.accelerate(50); // speed up only car2
car1.accelerate(10); // car1 again
car1.show();
car2.show();
}
}
class Car {
String brand;
int speed;
void accelerate(int amount) {
speed = speed + amount;
}
void show() {
System.out.println(brand + " is going " + speed + " km/h");
}
}

Two cars from one class, sped up separately:

  • car1.accelerate(...) only changes car1.speed.
  • Inside the method, speed means β€œthe speed of this object”.
  • Car one ends at 40, car two at 50. They never mix.

Output

Toyota is going 40 km/h
Honda is going 50 km/h

Class vs object, one more time

The class is written once. It is the plan. An object is created with new, and you can make as many as you like. One cutter, many cookies, each one separate. That is how one class gives you many objects, each with its own field values.

πŸ’₯ Null references and NullPointerException

A trap you will hit early: a reference that was never given an object with new.

  • A reference that points at nothing has the value null.
  • null means β€œthis reference points at no object”. The paper has no address.
  • Use the dot on a null reference and Java has nowhere to walk, so it stops with an error.

Watch it happen.

public class Main {
public static void main(String[] args) {
Student[] students = new Student[2]; // array of references, all null
// ❌ we never did students[0] = new Student();
System.out.println(students[0].name); // boom
}
}
class Student {
String name;
}

Why it breaks:

  • The array holds two Student references, but we never built any objects.
  • So students[0] is null. It points at nothing.
  • students[0].name means β€œwalk to the object and read its name”, but there is no object.

NullPointerException

Exception in thread "main" java.lang.NullPointerException: Cannot read field "name" because "students[0]" is null

This famous NullPointerException almost always means the same thing: you used the dot on a reference with no object. The fix is to build the object with new before you use it.

Student[] students = new Student[2];
students[0] = new Student(); // βœ… now there is a real object
students[0].name = "Alex";
System.out.println(students[0].name); // works fine

So whenever you see NullPointerException, ask: did I forget to create the object with new?

πŸ—οΈ Class versus object, said plainly

These two words get mixed up constantly. The difference:

  • A class is the blueprint, written once. It describes the fields and methods. You cannot store a value directly in it.
  • An object is built from the class with new. It lives on the heap, holds real values, and each one is independent.
  • In code, the class is what you write with the word class. The object is what new gives back.

Both appear together here.

Student s1 = new Student();
// ^^ ^^^^^^^
// object class

Student on the right is the class. new Student() gives the object. s1 is a name that points to it. One class, many possible objects.

⚠️ Common Mistakes

A few object-creation slip-ups to watch for.

  • Forgetting new. Student s1; declares a variable but builds no object. The reference points at nothing.
// ❌ no object was ever created
Student s1;
s1.name = "Alex"; // will not work, s1 has no object
// βœ… build the object first
Student s1 = new Student();
s1.name = "Alex";
  • Using a reference that is null. A reference pointing at nothing throws a NullPointerException the moment you use a dot on it. Build the object before you touch it.
// ❌ student is null, the dot has nowhere to go
Student student = null;
System.out.println(student.name);
// βœ… create the object first
Student student = new Student();
student.name = "Alex";
System.out.println(student.name);
  • Thinking objects share their fields. Each object has its own copy. Changing one does not change another.
Student s1 = new Student();
Student s2 = new Student();
s1.age = 20;
// ❌ thinking s2.age is now also 20 β€” it is not, it keeps its own value
  • Confusing the class with an object. The class is the blueprint. You do not store data directly on it. Create an object first, then use it.
// ❌ trying to use the class itself as if it held data
Student.name = "Alex";
// βœ… make an object, then set its field
Student s1 = new Student();
s1.name = "Alex";

βœ… Best Practices

Good habits for creating and using objects.

  • Always create with new before you use. This single habit removes most NullPointerException errors before they ever happen.
  • Create on the same line you declare, when you can. Writing Student s1 = new Student(); in one line means the variable is never left pointing at nothing.
  • Use clear variable names. alexAccount, firstCar, s1. The name should hint at which object it points to.
  • Make a fresh object for each separate thing. Two students means two new Student() calls. Do not try to reuse one object for two different people.
  • Remember = copies a reference, not an object. If you truly want a second independent object, you must call new again.

🧩 What You’ve Learned

You can now turn a blueprint into real objects. Quick recap.

  • βœ… You build an object from a class with the new keyword, like new Student().
  • βœ… An object lives on the heap, and the reference variable just points to it.
  • βœ… You reach an object’s fields and methods with the dot, like s1.name or s1.greet().
  • βœ… Each new makes a fresh, independent object, and each has its own copy of the fields.
  • βœ… Changing one object never changes another, because they do not share values.
  • βœ… A reference that points at nothing is null, and using a dot on it throws a NullPointerException.
  • βœ… The class is the one written blueprint, and objects are the many real things you build from it.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    Which keyword builds an object from a class?

    Why: You use new, like new Student(), to build an object from a class.

  2. 2

    What does the reference variable s1 actually hold?

    Why: The variable holds a reference, a pointer to where the object lives on the heap, not the object itself.

  3. 3

    After two new Student() calls, if you change s1.age, what happens to s2.age?

    Why: Each new makes an independent object with its own copy of the fields, so changing one does not affect the other.

  4. 4

    Why do you get a NullPointerException?

    Why: It happens when you use the dot on a null reference, meaning no object was ever built with new.

πŸš€ What’s Next?

You can now create objects and reach inside them with the dot. Next, look closer at what lives inside a class: data in fields, behavior in methods. There is more to know about both.

Java Fields and Methods

Share & Connect