Instance Constructor can be termed as a special method called the system runtime implicitly (automatically) as soon as an instance of the class is created.
So, what is the use of Instance Constructor? A primary function of instance constructor is to initialize attributes when creating objects.
How to create Instance Constructor? The instance constructor is a special instance method. To create an instance constructor, declare a method with name CONSTRUCTOR in class definition. Constructor can only have IMPORTING parameters and EXCEPTIONS.
Few points related to CONSTRUCTOR:
- Just like other method of the class, a constructor can access other components of the class
- A class can have only one constructor defined only under public section
- Inside constructor if an exception is raised, instantiation fails, and no main memory space is occupied.
Let’s understand the CONSTRUCTOR concept with the help of an example:
Class Definition.
Class implementation.
Create Objects
Output