Beginner 15 min read

What Is an Object?

By the end of this lesson, you'll be able to:

  • Define what an object (instance) is
  • Create an object from a class using the new keyword
  • Explain that multiple objects from the same class are independent of each other

Prerequisites: "What Is a Class?"

Creating an object with new

public class GymMember {
    public String name;
    public Integer age;
}

GymMember member1 = new GymMember();
member1.name = 'Zanele';
member1.age = 28;
  1. public class GymMember { — the blueprint, same as the previous lesson.
  2. public String name; — a field for the name.
  3. public Integer age; — a field for the age.
  4. } — closes the class.
  5. (blank line)
  6. GymMember member1 = new GymMember(); — new GymMember() builds one actual object from the blueprint, and member1 is a variable holding a reference to it.
  7. member1.name = 'Zanele'; — fills in that specific object's name.
  8. member1.age = 28; — and its age.

Independent objects from the same class

GymMember member1 = new GymMember();
member1.name = 'Zanele';

GymMember member2 = new GymMember();
member2.name = 'Kofi';

System.debug(member1.name); // Zanele
System.debug(member2.name); // Kofi
  1. GymMember member1 = new GymMember(); — creates the first object.
  2. member1.name = 'Zanele'; — sets its name.
  3. (blank line)
  4. GymMember member2 = new GymMember(); — creates a second, completely separate object.
  5. member2.name = 'Kofi'; — sets the second object's name — this has no effect on member1.
  6. (blank line)
  7. System.debug(member1.name); — logs Zanele.
  8. System.debug(member2.name); — logs Kofi.

member1 and member2 are both GymMember objects, built from the same class, but changing one never affects the other — exactly like two houses built from the same blueprint don't share walls.

Common mistakes

  • Forgetting new. GymMember member1; declares a variable that can hold a GymMember, but it starts out null — no object exists until new GymMember() actually creates one.
  • Assuming two variables always mean two objects. GymMember member3 = member1; doesn't create a new object — member3 and member1 both point to the same object, so changing one through either variable affects both.

Exercise

Given the GymMember class above, create two GymMember objects with different names and ages, and debug both names.

Show hint

GymMember member1 = new GymMember(); member1.name = ...;

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

What Is an Object? Quiz

1. What does the new keyword do?

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 1/1

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

An object is one actual, specific thing built from a class's blueprint — created with new, and independent of every other object built from the same class.