4/24/25, 3:08 PM Using Constructors | Apex Developer Guide | Salesforce Developers
Developers
Using Constructors
Apex Developer Guide / Writing Apex / Classes, Objects, and Interfaces / Classes / Using Constructors
Using Constructors
A constructor is code that is invoked when an object is created from the class blueprint. You do not
need to write a constructor for every class. If a class doesn't have a user-defined constructor, a
default, no-argument constructor with the same visibility as the containing class is generated.
The syntax for a constructor is similar to a method, but it differs from a method definition in that it
never has an explicit return type and it is not inherited by the object created from it.
After you write the constructor for a class, you must use the new keyword in order to instantiate an
object from that class, using that constructor. For example, using the following class:
public class TestObject {
// The no argument constructor
public TestObject() {
// more code here
}
}
A new object of this type can be instantiated with this code.
TestObject myTest = new TestObject();
If you write a constructor that takes arguments, you can then use that constructor to create an
object using those arguments.
If you create a constructor that takes arguments, and you still want to use a no-argument
constructor, you must create your own no-argument constructor in your code. After you create a
constructor for a class, you no longer have access to the default, no-argument public constructor.
In Apex, a constructor can be overloaded, that is, there can be more than one constructor for a
class, each having different parameters. This example illustrates a class with two constructors: one
with no arguments and one that takes a simple Integer argument. It also illustrates how one
constructor calls another constructor using the this(...) syntax, also know as constructor
chaining.
public class TestObject2 {
private static final Integer DEFAULT_SIZE = 10;
Integer size;
//Constructor with no arguments
public TestObject2() {
this(DEFAULT_SIZE); // Using this(...) calls the one argument constructor
}
// Constructor with one argument
public TestObject2(Integer ObjectSize) {
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm 1/3
4/24/25, 3:08 PM Using Constructors | Apex Developer Guide | Salesforce Developers
size = ObjectSize;
}
}
New objects of this type can be instantiated with this code.
TestObject2 myObject1 = new TestObject2(42);
TestObject2 myObject2 = new TestObject2();
Every constructor that you create for a class must have a different argument list. In this example, all
of the constructors are possible.
public class Leads {
// First a no-argument constructor
public Leads () {}
// A constructor with one argument
public Leads (Boolean call) {}
// A constructor with two arguments
public Leads (String email, Boolean call) {}
// Though this constructor has the same arguments as the
// one above, they are in a different order, so this is legal
public Leads (Boolean call, String email) {}
}
When you define a new class, you are defining a new data type. You can use class name in any
place you can use other data type names, such as String, Boolean, or Account. If you define a
variable whose type is a class, any object you assign to it must be an instance of that class or
subclass.
DID THIS ARTICLE SOLVE YOUR ISSUE?
Share your feedback
Let us know so we can improve!
DEVELOPER CENTERS POPULAR RESOUR CES COMMUNITY
Heroku Documentation Trailblazer Community
MuleSoft Component Library Events and Calendar
Tableau APIs Partner Community
Commerce Cloud Trailhead Blog
Lightning Design System Sample Apps Salesforce Admins
Einstein Podcasts Salesforce Architects
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm 2/3
4/24/25, 3:08 PM Using Constructors | Apex Developer Guide | Salesforce Developers
Quip AppExchange
© Copyright 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce, Inc.
Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
Privacy Information Terms of Use Legal Use of Cookies Trust Cookie Preferences
Your Privacy Choices Responsible Disclosure Contact
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm 3/3