Working with local classes Flashcards
What are global classes in ABAP and how are they stored?
Global classes in ABAP are stored centrally in a class pool and contain reusable logic for other ABAP programs.
What are local classes in ABAP and where are they defined?
defined within a program or a global class, restricted to use within their defining scope.
How can you quickly create a new local class
typing “lcl” and then pressing strg+leertaste, adjust name of the class
What are the two parts of code of a class called
Definition and implementation. The definition part of a class is subdivided into up to three sections, called the visibility sections of the class (public, protected, private section).
What does the TYPES statement allow within a class in ABAP?
defining types within a class, similarly to outside a class. If types are public, programs using the class can use these types for their variables.
How do you declare attributes in an ABAP class?
Attributes in an ABAP class are declared using DATA for instance attributes and CLASS-DATA for static attributes.
How are constants declared within ABAP classes?
Constants within ABAP classes are declared using CONSTANTS. They, along with types, are static components of the class.
How are methods defined within ABAP classes?
METHODS for instance methods and CLASS-METHODS for static methods. The method’s name is followed by its signature, including the values exchanged with its caller and possible exceptions..
How do you access the local classes within your globall class
on the bottom below the code editor are different tabs. One of them is called local types. Create a new local class by typing lcl and then strg+leer there. Remember to give it a name (“lcl_…”)
Inside the class you work with attributes like with normal variables. How do you access static attributes outside though?
<class_name>=><static_attribute>
lcl_connection=>conn_counter = 5.
no blank spaces!
"=>" is called the static component selector
</static_attribute></class_name>
What is a reference variable
a special variable that is used to create, address and manage an object. It points at the instance of a class in the program memory
How do you declare a reference variable
DATA <referenceVariable> TYPE REF TO <className></className></referenceVariable>
How do you create a new instance of a class
With the NEW operator.
connection = NEW #( )
the hashtag means use the type of the variable before the equals sign, which is declared through the DATA … TYPE REF TO … part.
It is also possible to replace the hashtag with the name of the class
What is the difference between a normal and a static attribute
A normal attribute is unique for each single object. A static one is shared between each object. So if one object changes a static attribute, that value is simultaneously changed for all other objects of that type
Static components are adressed with the classname and “=>”. How are instance components attributes adressed?
With the instance component selector “->”:
<reference_variable>-><attribute>
connection->carrier_id = 'LH'.
</attribute></reference_variable>
What is the consequence if you code sth. like this
con = NEW #( ).
con2 = NEW #( ).
they both point to the same memory address. So if you change one of them, you “change the other one” as well
What happens when you write:
con = NEW #( ).
con = NEW #( ).
The reference variable will no longer point to the first object, but rather to the second oe. Therefore, nothing points to the object generated during the first call. It will therefore be deleted by the garbage collector.
How could you prevent the deletion of an object even though you use con = NEW #( ).
several times?
By saving it in an internal table
How do you create new methods?
Within the public/private/protected section of a class:
METHODS method_name
IMPORTING
i_helper TYPE i.
EXPORTING
e_helper TYPE i.
Importing is what variables are being fed to the method and export vice verca. These methods need an implementation in the IMPLEMENTATION section
When a piece of code is underlined in red squiggly lines, how do you access the quick fix menu for it
Strg + 1, or right click -> quick fix
What kind of attributes are instance and static methods allowed to access
instance methods can access instance and static attributes.
Static methods can only access static attributes.
Within the implementation of instance methods, ABAP offers the built in variable “ME”. Describe its purpose
ME is a reference variable, typed with the current class and filled at runtime with the address of the current instance. The use of ME is optional and should be avoided, unless the name of an attribute collides with the name of another data object, for example a parameter name.
Example:
Definition part of the class:
DATA carrier_id TYPE i.
…
METHODS set_attributes
IMPORTING
carrier_id TYPE i
now we have the case, where the parameter carrier_id in METHODS has the same name as the attribute carrier_id. The name itself would trherefore denote the parameter. To access the attribute of the current instance, we need “me->”:
Implementation part of the class:
METHOD set_attributes.
me->carrier_id = carrier_id.
How can a method raise an exception
Within the method body, you can type:
RAISE EXCEPTION TYPE <exception_class>.
Execution will be terminated and control returned to method caller, if possible (raising clause, try endtry and catch block need to exist. quickfix can help with that)</exception_class>
How are instance and static methods called
Instance methods:
<reference_variable>-><method_name>(
EXPORTING
IMPORTING
CHANGING
).
Static methods:
<class_name>=><static_method>(
...
).
</static_method></class_name></method_name></reference_variable>