Monday, February 11, 2013

Global Data in OOPs Concept

Global data is accessible from any class in a program. We know that the visibility depends on the public, protected or private section. The globally declared variable is visible from anywhere in the program. We have created a program where the global variable is v_globe and in the debugging mode we can see its value from any of classes.

REPORT  zsr_test NO STANDARD PAGE HEADING.

DATA: v_globe TYPE char40 VALUE 'This is Global Data'.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    DATA v_pub_1 TYPE char40 VALUE 'Class 1 public data'.
    METHODS m_cls1.

  PROTECTED SECTION.
    DATA v_pro_1 TYPE char40 VALUE 'Class 1 protected data'.

  PRIVATE SECTION.
    DATA v_pri_1 TYPE char40 VALUE 'Class 1 private data'.
ENDCLASS.                    "cls1 DEFINITION
*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
  PUBLIC SECTION.
    DATA v_pub_2 TYPE char40 VALUE 'Class 2 public data'.
    METHODS m_cls2.

  PROTECTED SECTION.
    DATA v_pro_2 TYPE char40 VALUE 'Class 2 protected data'.

  PRIVATE SECTION.
    DATA v_pri_2 TYPE char40 VALUE 'Class 2 private data'.
ENDCLASS.                    "cls2 DEFINITION
*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD m_cls1.
    WRITE: / v_pub_1,
           / v_pro_1,
           / v_pri_1.
  ENDMETHOD.                                                "m_cls1
ENDCLASS.                    "cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD m_cls2.
    WRITE: / v_pub_2,
           / v_pro_2,
           / v_pri_2.
  ENDMETHOD.                                                "m_cls2
ENDCLASS.                    "cls2 IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj1 TYPE REF TO cls1,
        obj2 TYPE REF TO cls2.
  CREATE OBJECT: obj1, obj2.
  CALL METHOD: obj1->m_cls1,
               obj2->m_cls2.

Now we have set the debugger at the CALL METHOD. The method of first class can access public, protected & private variables and the global variable from that first class implementation.


Similarly when the program enters to the second class implementation, the method can access all variables of that class and the global variable.


When the program is not under any class then also global variable can be accessed. 


The output is as follows.

7 comments:

yektek training said...

really nice post thanks

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

sanayamohit said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

Unknown said...

Best SAP Success Factors Training Institute in Chennai
Best SAP MM Training in Chennai
Best SAP SD Training in Chennai
Best SAP ABAP Training in Chennai
Best SAP FICO Training in Chennai
Best SAP BASIS Training in Chennai

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
http://thecreatingexperts.com/sap-mm-training-in-chennai/
http://thecreatingexperts.com/sap-sd-training-in-chennai/
http://thecreatingexperts.com/sap-hr-training-in-chennai/
http://thecreatingexperts.com/sap-fico-training-in-chennai/
http://thecreatingexperts.com/sap-abap-training-in-chennai/
http://thecreatingexperts.com/sap-basis-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

Keerthi Suresh said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

gilledashivani said...

Informative article. Thank you for sharing the great blog. You have done great explanation and visualization. Keep up the good work.
BEST SAP ABAP TRAINING IN HYDERABAD