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: 

Memory Allocation For a Variable Using Normal ABAP and OOPS ABAP

Former Member
0 Kudos

Hi,

I have created a basic program using oops...

   CLASS C1 DEFINITION.

  PUBLIC SECTION.
    DATA : W_TEXT(20) TYPE C VALUE 'oops basics'.
    METHODS : DISPLAY.
ENDCLASS.               

CLASS C1 IMPLEMENTATION.
  METHOD: DISPLAY.
    W_TEXT = 'CHANGED VALUE'.
    WRITE : / W_TEXT.
  ENDMETHOD.                    "DISPLAY
ENDCLASS.                    "c1 IMPLEMENTATION

START-OF-SELECTION.
  DATA : O_C1 TYPE REF TO C1.

  CREATE OBJECT O_C1.

  WRITE : / O_C1->W_TEXT.

           call method o_c1->display.

In the above program as you all see iam creating variable W_TEXT in the public section of the class which has visbility all the people..

now later iam creating and object isntance for the class

CREATE OBJECT O_C1.

now after the system processing the statement

CREATE OBJECT O_C1.

when iam check the value of w_text in debugging i cant see the scope of the variable w_text..

but instead if i see O_C1->W_TEXT

i can see the default value which i have given ie : 'OOPS BASICS'.

as per my knowledge the memory allocation will be done when we create a instance of the class.. but when the scope of the varlable W_TEXT is not yet defined after the statment O_C1->W_TEXT.

ie : since i have used WRITE : / O_C1->W_TEXT.

Its writing the value of w_text in the output... but in dubugging the scope for W_TEXT is not defined...

why is it so....... ? when we comapred with normal abap ie : let us say we have defined

DATA : WAR2(9) TYPE C VALUE 'SAMPLE'.

now if we check in the debugging the scope of the WAR2 will be defined..... ie : it will have the default value 'SAMPLE',

But how will it work in OOPS...

Can any one tell this...

Regards

Kumar

Edited by: Matt on Aug 27, 2009 2:27 PM - added tags

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hello Kumar,

I am not sure whether I have understood the question. In your example, you have defined a class with an instance attribute. As you have said, the memory for this instance is allocated with the instance creation (CREATE OBJECT). Afterwards, the instance attribute is present in memory.

In debugging, you can see the following:

- If the instance is not yet created (or more exactly: if your object reference does not point to an instance), the value of the object reference is . In your sample, O_C1 should have this value before the CREATE OBJECT statement is reached.

- Attributes of the object reference are not available in the debugger since the reference does not point to an instance. If O_C1 is initial, the attributes of O_C1, like 0_C1->W_TEXT, are undefined.

- After the object instance has been created through CREATE OBJECT, the object reference points to that instance. In the debugger, this looks similar to . Now the memory for all instance attributes is allocated.

- Thus, in the debugger, the instance attribute O_C1->W_TEXT is defined now (with its default value).

If you enter W_TEXT into your debugger, the debugger looks for a field with this name in the current context. As long as the context is not an instance method of your class, the debugger will not find such a field. In the context of START-OF-SELECTION, such a field is not defined. However, if you debug your instance method DISPLAY, the field is visible.

I hope this was the answer to your question...

Regards

David

8 REPLIES 8

matt
Active Contributor
0 Kudos

When you're inside the method call, w_text is visible. Outside of the method, you have to give the object reference. ( Even within the method it's a good idea to use the me object reference - i.e. me->w_text.)

It's a bit like with a structure field. You can't use matnr on its own - you have to use mara-matnr. Accessing an objects attributes outside of the class, you must use objref->attribute.

matt

Former Member
0 Kudos

Hello Kumar,

I am not sure whether I have understood the question. In your example, you have defined a class with an instance attribute. As you have said, the memory for this instance is allocated with the instance creation (CREATE OBJECT). Afterwards, the instance attribute is present in memory.

In debugging, you can see the following:

- If the instance is not yet created (or more exactly: if your object reference does not point to an instance), the value of the object reference is . In your sample, O_C1 should have this value before the CREATE OBJECT statement is reached.

- Attributes of the object reference are not available in the debugger since the reference does not point to an instance. If O_C1 is initial, the attributes of O_C1, like 0_C1->W_TEXT, are undefined.

- After the object instance has been created through CREATE OBJECT, the object reference points to that instance. In the debugger, this looks similar to . Now the memory for all instance attributes is allocated.

- Thus, in the debugger, the instance attribute O_C1->W_TEXT is defined now (with its default value).

If you enter W_TEXT into your debugger, the debugger looks for a field with this name in the current context. As long as the context is not an instance method of your class, the debugger will not find such a field. In the context of START-OF-SELECTION, such a field is not defined. However, if you debug your instance method DISPLAY, the field is visible.

I hope this was the answer to your question...

Regards

David

0 Kudos

Hi david....

You mean when the system encounters the statement CREATE OBJECT O_C1... The object instance will be created an some memory will be allocated to it in the ram.... Since the variable w_text is the attribute of the Object O_C1 so the varible address goes and sits in that memory location in which the object has been created...

if the above one is correct then obviously we need to first refer to the object which in turn refers to the attribute.....

if we directly refer to the attribute without refering to the object so the system does not find the address location of the attribute...

I am I correct...?

correct me if I am wrong..

Regards

kumar

0 Kudos

is that correct ....?

Regards

kumar

Edited by: kumar.sap143 on Aug 27, 2009 8:17 PM

0 Kudos

Hello Kumar,

The simple thing is there is not way to directly access class members. You have to create instance of it and then you can access public members. It is same like structures only thing is visibility control is add to it which is nothing but encasulation.

Refer to SAP Help for more details on OOPS!

Hope this helps!

Thanks,

Augustin.

matt
Active Contributor
0 Kudos

Kumar, largely it's correct.

When you create an instance of a class, a portion of memory, defined by the attributes of the class, is set aside for the object on the heap. All you get is a reference variable to that portion. ( Sometimes, this is called a pointer).

DATA: my_ref1 TYPE REF TO myclass,
     my_ref2 TYPE REF TO myclass.

CREATE OBJECT my_ref1.
my_ref1->my_attributer = 'fish'.

my_ref2 = my_ref1.

WRITE: / my_ref2-my_attribute

my_ref2 refers to (points to) exactly the same part of memory as my_ref1.

matt

0 Kudos

Hello Kumar,

what you wrote is correct: If you want to access a public instance attribute from outside the class (as in your example), you must specify the concrete object instance from which you want to access the attribute. That is a basic concept of object orientation. To give you a small example: You have a class "CAR" which has an attribute "COLOUR". In your program, you create multiple instances of this class. Each instance has its own colour attribute. Therefore, you cannot refer to "COLOUR" without specifying which concrete car's colour is meant. However, if you have an object reference GO_FERRARI which points to an instace of "CAR", you can access its colour attribute by using GO_FERRARI->COLOUR.

Besides instance attributes, classes can also have static attributes (defined by the CLASS-DATA statement). These attributes do not belong to a concrete instance (i.e. a concrete car), but to all instances of the class. Public static attributes can be accessed from outside the class by using CLASSNAME=>ATTRIBUTE. These attributes are initialized before the class is used for the first time (in an internal mode).

Hope this solves your question,

David

0 Kudos

Thanks Guys... for ur valuble answer....!!!!!!1