What is Constructor and Types of Constructors in Apex Class
Constructor:
Constructor is a special method which have the properties.
I. Method name will be same as class.
II. Access specifies will be public.
III. This method will involve only once that is at the time of creating an abject.
IV. This is used to instantiate the data members of the class.
Ex:
Public class test object
{
\\ the no argument constructer
Public test object ()
{
// code
}
}
There are 3types of constructors.
I.Default constructor
II.Non – parameterized constructor
III.Parameterized constructor
1)Default constructor:
If an apex class doesn’t contain any constrictor, then apex compiler by default. Creates a dummy constructor on the name of class when we create an object for the class.
Ex: public class example
{
}
Example e = new example ();
In the above example the apex class doesn’t contain any constrictor. So when we create object example class the apex compiles creates a default constructor.
Public example ()
{
}
2)Non – parameterized constructor & 3) parametrized constructor:
It is constrictor that doesn’t have any parameters, or constructor that has parameters.
Ex:
Public class example
{
Integer rno;
String name;
Public example (integer x, String my name)
{
rno = x,
name = my name;
}
Public example ()
{
//code
}
}
No comments:
Post a Comment