Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

About CLASS-METHODS?

Former Member
0 Kudos

Hi Guys,

In ABAP objects i have seen while declaring methods, they are prefixing CLASS-METHODS?

Actually what does that mean?

Regards,

Ammu

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Amruth,

CLASS-METHODS is used while declaring the static methods.

CLASS-DATA is used while declaring static attributes.

Static methods can access only static attributes.

Reward points if it is useful.

Regards,

Chandru

8 REPLIES 8

Former Member
0 Kudos

Hi Amruth,

CLASS-METHODS is used while declaring the static methods.

CLASS-DATA is used while declaring static attributes.

Static methods can access only static attributes.

Reward points if it is useful.

Regards,

Chandru

0 Kudos

What is the use of static methods?

0 Kudos

Hi,

A static method can be called without prior instantiation of the class. If the method does not need to know any details of an instance then we can define the method as static.

Static methods can only use static components in their implementation part.Static methods are called using CALL METHOD <classname>=><class_method>.

If you are calling a static method from within the class, you can omit the class name.

You access static attributes using <classname>=><class_attribute>

Regards,

Omkar.

0 Kudos

You can only use this statement in the declaration part of a class definition (see CLASS ) or in an interface definition (see INTERFACE). The CLASS- prefix to the METHODS statement means that the method meth is declared as a static method. Otherwise, the CLASS-METHODS statement has the same syntax as the METHODS statement for instance methods.

Static methods belong to the static components of a class. Unlike instance methods, which can access both instance and static attributes, and trigger both instance and static events, static methods can only access static attributes (see CLASS-DATA), and can only trigger static events (see CLASS-EVENTS). This makes static methods independent of instances, and callable even when no instances of a class exist. Static methods work like global functions, restricted to the context of a class. You use them to work with the contents of static attributes, or for tasks that affect the entire system.

Static methods cannot be redefined in a subclass. For this reason, you do not need to declare them as abstract or final methods. The REDEFINITION, ABSTRACT, and FINAL additions of the METHODS statement are not allowed in the CLASS-METHODS statement.

Within a class, static methods have the same appearance as instance methods, and can be addressed in the same way. They can also be addressed from outside the class if their visibility allows. When you address a static method from outside its class, you can use either an object reference or the class name. For example, if meth is a static method of the class class , you can address it using CALL METHOD class=>meth in any program in which the class class is declared.

0 Kudos

Hi Chandra,

Because You replied telling that CLASS-METHODS triggers CLASS-EVENTS.

BTW What does class-events mean?

Regards,

Amruth

0 Kudos

Declares static events in a class or interface in ABAP Objects.

You can only use this statement in the declaration part of a class declaration (see CLASS ) or interface definition (see INTERFACE). The CLASS- prefix to the EVENTS statement means that the event evt is declared as a static event. Otherwise, the CLASS-EVENTS statement has the same syntax as the EVENTS statement for instance events.

Static events belong to the static components of a class. Unlike instance events, that can only be triggered in instance methods, static events can be triggered both in instance methods and in static methods.

Within a class, the appearance of static events is the same as that of instance events, and they can be addressed in the same way. You can also define handler methods for static events using the SET HANDLER statement if their visibility allows it. In this case, there is no FOR addition, since the required information is already available through the definition of the event handler method, and you do not have to specify a triggering instance.

Note

If the class has a static constructor (a method called CLASS_CONSTRUCTOR) that has not yet been executed in the program, it is executed on a static event in the class before the first registration of an event handler method.

Example

CLASS C1 DEFINITION.

PUBLIC SECTION.

METHODS CONSTRUCTOR.

CLASS-METHODS CREATE_COUNT_RESET.

CLASS-EVENTS COUNT_RESET.

PRIVATE SECTION.

CLASS-DATA CREATE_COUNT TYPE I.

ENDCLASS.

CLASS C2 DEFINITION.

PUBLIC SECTION.

METHODS COUNT_HANDLER FOR EVENT COUNT_RESET OF C1.

ENDCLASS.

DATA: O1 TYPE REF TO C1,

O2 LIKE O1,

O3 LIKE O1,

HANDL TYPE REF TO C2.

CREATE OBJECT: O1,

O2,

O3,

HANDL.

SET HANDLER HANDL->COUNT_HANDLER.

CLEAR: O1,

O2,

O3.

CALL METHOD C1=>CREATE_COUNT_RESET.

CLASS C1 IMPLEMENTATION.

METHOD CONSTRUCTOR.

CREATE_COUNT = CREATE_COUNT + 1.

ENDMETHOD.

METHOD CREATE_COUNT_RESET.

CLEAR CREATE_COUNT.

RAISE EVENT COUNT_RESET.

ENDMETHOD.

ENDCLASS.

CLASS C2 IMPLEMENTATION.

METHOD COUNT_HANDLER.

WRITE 'Object counter reset'.

ENDMETHOD.

ENDCLASS.

This example builds on the first example in the CLASS-METHODS documentation. It is possible to execute the static method CREATE_COUNT_RESET, even though there are no more instances of class C1. The COUNT_HANDLER method of object HANDL of class C2 is registered as handler for the static event COUNT_RESET, and is triggered.

Former Member
0 Kudos

No answer for the other question

0 Kudos

Hi

Static methods work like global functions, restricted to the context of a class.

static methods can only access static attributes and can only trigger static events

Thanks

Ravi

Message was edited by:

Ravikanth Alapati