You are on page 1of 9

Working with constructors (Global Class

)

By Sravanthi

1. Constructors are special methods that cannot be called using CALL METHOD. Instead, they are
called automatically by the system to set the starting state of a new object or class.

2. Constructors are methods with a predefined name. To use them, you must declare them
explicitly in the class.

3. There are two types of constructors - Instance constructors and Static constructors.

 The Instance constructor of a class is the predefined instance method CONSTRUCTOR.
 The constructor's signature can have only importing parameters or exceptions.
 Instance Constructor is a special instance method can access the Instance Variables and
Static Variables.
 This method is used to set the default values in a class.

 The Static constructor of a class is the predefined static method CLASS_CONSTRUCTOR.
 The constructor's signature cannot have importing parameters or exceptions.
 The static constructor is as special static method can access the static variables.
 These are used to set default values for the global attributes irrespective of instances.

 Multiple Instance and Static Constructors cannot be used in one class.

Creating the Instance Constructor and Static Constructor using SE24 (Global class)

Step1: Go to SE24  Create Global Class ZCONSTRUCTOR_SALES




Step2: Select the Class and Click on Continue.



Step3: Continue with the default settings



Step4: Go to Methods  Create Methods.



Step5: Constructor Method can be created 2 ways.
1) Go to Menu  Edit  Create Constructor
2) Click on Constructor as shown below


Step6: As soon click on Constructor  Constructor Method is automatically defaulted in the
Methods.
This method is used o initial the values for the object


Step7: Create another method Select to fetch the data from the VBAK table



Step8: Select the Constructor Method  Select the Parameters and Create the Parameter



Step9: The parameter IM_VBELN is used to importing into Constructor Method. Used to initialize
the value



Step10: For the Constructor, parameter type is always importing. No other Parameter type can be
assigned



Step11: Go to Type Tab  Create Structure ty_vbak and Visibility (Public)




Step12: Click on Direct Type Entry

Step13: To declare the Structure (with required fields) and Table

Step14: t_vbak is defaulted in the Types tab which has be declared in earlier step

Step15: In the Attributes Tab Create V_VBELN type vbak-vbeln.

Step16: Double click the Constructor and Implement the code.

Step17: Go to Select Method  Create Parameters






Step18: Go to Select Method  Double Click the Method and Implement the code

Step19: Activate the Class and Go to SE38  Create a Program: ZCONSTRUCTOR_SALES.
Implement the code and execute the Program


Step20: Execute the Program without giving any sales document number.

Step21: The Output is displayed as shown below with the Constructor method with the default
initial values

Step22: Give the Sales Document as: 4971

Step23: The output is displayed for the corresponding Sales document


Static Constructor
Step24: Within the Same Global Class Create the Static Constructor. Click on Class Constructor
OR
Menu  Edit  Create Class Constructor.