function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anil Kumar 630Anil Kumar 630 

Is it mandatory to use constructor in apex classs?? what exactly constructor does..??how it works??? plz explain in simple terms

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Anil Kumar,

A constructor is a 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 does not have a user-defined constructor, an implicit, no-argument, public one is used.

Please refer the link below.
http://sfdcsrini.blogspot.com/2014/04/what-is-constructor.html

Hope it will be helpful.

Best Regards
Rahul Kumar
Akshay_DhimanAkshay_Dhiman
Hi Anil,

A Constructor is a block of code similar to a method that’s called when an instance of an object is created. Here are the key differences between a constructor and a method:

1) A constructor doesn’t have a return type.

2) The name of the constructor must be the same as the name of the class.

3) Unlike methods, constructors are not considered members of a class.

4) A constructor is called automatically when a new instance of an object is created.

5) The public keyword indicates that other classes can access the constructor. ClassName must be the same as the name of the class that contains the constructor.

A constructor allows you to provide initial values for class fields when you create the object. Suppose that you have a class named Actor that has fields named firstName and lastName. You can create a constructor for the Actor class:
public Actor(String first, String last){
    firstName = first;
    lastName = last;
}
Then you create an instance of the Actor class by calling this constructor:
Actor a = new Actor("Arnold", " Schwarzenegger");
A new Actor object for Arnold Schwarzenegger is created.

Like methods, constructors can be overloaded. In other words, you can provide more than one constructor for a class if each constructor has a unique signature. Here’s another constructor for the Actor class:
public Actor(String first, String last, boolean good){
    firstName = first;
    lastName = last;
    goodActor = good;
}
This constructor lets you create an Actor object with information besides the actor’s name:
Actor a = new Actor("Arnold", "Schwarzenegger", false);
If you do not provide a constructor for a class, Salesforce will automatically create a default constructor that has no parameters and doesn’t initialize any fields. This default constructor is called if you specify the new keyword without passing parameters. For example:
Ball b = new Ball();
Here, a variable of type Ball is created by using the default constructor for the Ball class.
If you explicitly declare any constructors for a class, Salesforce does not create a default constructor for the class. As a result, if you declare a constructor that accepts parameters and still want to have an empty constructor (with no parameters and nobody), you must explicitly declare an empty constructor for the class.

Regards,
Akshay