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: 

Calling an instance method from static method?

ajaygupta
Participant
0 Kudos

Problem statement : Is it possible to access an instance method from a static method in ABAP ?. (In java it is not possible).

I tried to analyse this problem statement with the below sample code. After execution I CONCLUDED that it is possible but still need to confirm...

*&---------------------------------------------------------------------*

*& Report  ZCLASS1

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  zclass1.

*----------------------------------------------------------------------*

*       CLASS oops3 DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS oops3 DEFINITION.

   PUBLIC SECTION.

     METHODS       : m_inst1.

     CLASS-METHODS : m_stat1.

ENDCLASS.                    "oops3 DEFINITION

*----------------------------------------------------------------------*

*       CLASS oops3 IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS oops3 IMPLEMENTATION.

   METHOD m_inst1.

     WRITE : 'I was called from method m_stat1'.

   ENDMETHOD.                                                "m_inst1

   METHOD m_stat1.

     DATA : insideobj TYPE REF TO oops3.

     CREATE OBJECT insideobj.

     CALL METHOD insideobj->m_inst1.

   ENDMETHOD.                                                "m_stat1

ENDCLASS.                    "oops3 IMPLEMENTATION

START-OF-SELECTION.

CALL METHOD oops3=>m_stat1.

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos

Of course it works, why do you need a confirmation? The most known example is the "factory" method whose goal is to return an instance. It may be forbidden of creating an instance (CREATE OBJECT) from outside by declaring the class with the private or protected instantiation flag.

1 REPLY 1

Sandra_Rossi
Active Contributor
0 Kudos

Of course it works, why do you need a confirmation? The most known example is the "factory" method whose goal is to return an instance. It may be forbidden of creating an instance (CREATE OBJECT) from outside by declaring the class with the private or protected instantiation flag.