Methods, Types, and Attributes of a Global
Class
You have now defined the basic attributes of the class MY_EXMAMPLE_CLASS through the menu. Below you define the methods, attributes, and types of the class. The class should have the following methods:
Method |
Description |
SAVE_NAME |
Takes a name consisting of a first and last name as input parameters, and enters it into the database table MYTABLE. |
GET_MAX_INDEX |
Returns the highest value of the ID column of the table MYTABLE. This is an additional function of SAVE_NAME and helps to ensure that the new table entry gets an ID that does not yet exist. |
WRITE_NAMES |
Outputs all the names for the table MYTABLE. |
SEARCH_NAME |
Checks whether a name already exists in the table MYTABLE. Depending on this, it returns 0 or 1. |
CLEAR_TABLE |
Clears the table MYTABLE. |
If you double-click the class name in the tree in the left lower window of SE80, a view of the Class Builder opens in the window on the right. You will see how you can define a method there, step by step, through the menu. You can do the same for the other methods on your own.

Now create the method SAVE_NAME with an input parameter.
...
1. First switch to change mode by choosing the Display/Change icon. The background color of the lines in which you can now enter something changes to white.
2. In the Methods tab under the Methods header, enter the method name.
Click on the field with the column heading Level.

A circle opens up at the end of the field. This leads you to an input help.
This symbol is the symbol used throughout the system for input help. If you click it, you will see a dialog box in which you can choose the allowed options from the displayed list.
3. Choose Instance Method. It would also suffice if you did not open the dialog box, but typed the number assigned to the desired selection – in our case zero – into the appropriate field. If you know the number, you will save yourself time. It is quicker to write “zero” than Instance Method.

4. Choose Public under Visibility and enter a short description of the method.
5. Before you can enter an input parameter for this method, you require a data type for it. Under the Types tab, enter the name MY_NAME, visibility Public, and click on the yellow arrow. Now you get to the Public section, where you can enter the coding for the required data type. You need a structure with the first component FIRSTNAME and the second SECONDNAME. For the respective type of component, you can use the structure that you defined at the beginning in the ABAP Dictionary.

6. In the Public section, you will find the coding type MY_NAME. Since you require a structured type, change the coding as follows:
TYPES: begin of my_name,
firstname
type myfirstname,
secondname type mysecondname,
end of my_name.
7. Now return to the Methods tab, push the Parameters button, and enter the parameter P_NAME. Choose Importing as type and select the field Pass Value.

8. Under Associated Type, choose a data type for the parameter. Enter the type MY_NAME.
9. Using the button with the blue arrow, return to the method overview, double-click the method name, and enter the implementation of the method:
METHOD save_name.
DATA struc_my_table TYPE mytable.
struc_my_table-id =
get_max_index( ) + 1.
struc_my_table-firstname = p_name-firstname.
struc_my_table-secondname = p_name-secondname.
INSERT INTO mytable VALUES struc_my_table.
ENDMETHOD.
In the second line, you can see perfectly how easy it is to get a work area in an ABAP program for the work with a database table Assign to a data field the type of the database table as data type. You get a structure wit the line type of the database table. In the next line, the function GET_MAX_INDEX( ) supplies you with the highest index value of the database table. This ensure that the new entry gets a unique index value. In the two next lines, you fill in the respective components of the work area structure with the appropriate components of the input parameter, and then you enter the line into the database table.
Just like you have seen with the SAVE_NAME method, now create the remaining methods as public instance methods. If possible, choose the data pass Value for the parameters.
...
1. The method GET_MAX_INDEX with the return parameter MAXIMAL of type i:
METHOD get_max_index.
DATA id TYPE i.
SELECT MAX( id ) FROM mytable INTO id.
maximal = id.
ENDMETHOD.
2. The method WRITENAMES without parameters:
METHOD write_names.
DATA: itab TYPE STANDARD TABLE OF mytable,
itab_line LIKE LINE OF itab.
SELECT * FROM mytable INTO TABLE itab.
LOOP AT itab INTO itab_line.
WRITE: AT 5 itab_line-id,
AT 15 itab_line-firstname,
AT 35 itab_line-secondname.
SKIP.
ENDLOOP.
ENDMETHOD.
You select the data in an internal table and output these, line for line, using the WRITE command.
Note: The WRITE command should be used as little in modern, productive programs in ABAP as the console output function in Java. You can use it for test purposes at any time, as in our program. There are more modern technologies available in ABAP for state-of-the-art user output.
3. The method SEARCH_NAME with the import parameter p_name_searched of the type MY_NAME and the return parameter p_result of the type RETURNVALUE. You create this type in the Class Builder as a public type using TYPES: RETURNVALUE(1) TYPE in the same way as described above for the type MY_NAME:
METHOD search_name.
DATA l_id TYPE myid.
SELECT SINGLE id FROM mytable
INTO l_id
WHERE firstname = p_name_searched-firstname AND
secondname = p_name_searched-secondname.
IF sy-subrc = 0.
* write 'successful'.
p_result = 0.
ELSE.
* write 'not sucessful'.
p_result = 1.
ENDIF.
ENDMETHOD.
Select the ID of the data record in question from the database. If the name exists, the system field SY-SUBRC is set to zero by the system; otherwise, it is set to another value. You can also choose a test output.
4. The method CLEAR_TABLE without parameters:
METHOD clear_mytable.
DELETE FROM mytable.
ENDMETHOD.