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: 

abap oo constructor prob

Former Member
0 Kudos
data: g_dock type ref to cl_gui_docking_container,
        g_split type ref to cl_gui_easy_splitting_container,
        g_cont type ref to cl_gui_container.
 
create object g_dock       " this is a constructor
  exporting
     *side = cl_gui_docking_container=>dock_at_top*      ..... "pls explain this syntax in general
extension = 200.

'dock_at_top' is a constant in the attributes component of the class 'cl_gui_docking_container'.

1.) we are already creating an instance of this class as 'g_dock'.

so instead of writing 'g_dock -> dock_at_top', why are we using ' clgui_docking_container=>dock_at_top_ '. pls explain this.

2.) pls clarify 1 thing ....the syntax ...

"create object g_dock

exporting

......... "

it is a constructor method, so why are we writing 'cl_gui_docking_container=>dock_at_top' instead of writing only 'dock_at_top'

pls help.

detailed answers will be appreciated, thnks in adv

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Clarification,

By saying

CREATE OBJECT <object_name>
                   EXPORTING 
                       <par1> = <val>
                       <par2> = <val>

What we are doing here is instanciate an object and send the constructor values that it is expecting on the constructor's import section, if we did not have paramaters expected on the constructor of class

cl_gui_docking_container

,

we would just say

CREATE OBJECT <object_name>

*as for *

cl_gui_docking_container=>dock_at_top

You could just have

DOCK_AT_TOP

alone if you want, but, yes there is a but!

It will come back to bite you, why,

class components are defined in terms of a given class

and most probably donu2019t have a lot of meaning outside of their defining classu2019s context. To avoid this kind of confusion, it is recommended that you always address class components by applying the class component selector to the defining

classu2019s name. Therefore,

cl_gui_docking_container=>dock_at_top

instead of just

dock_at_top

That way, your intentions are always clear.

I hope this makes sence.

9 REPLIES 9

uwe_schieferstein
Active Contributor
0 Kudos

Hello

Within an instance of the docking container class itself the constant DOCK_AT_TOP is known.

The piece of coding is presumably within the CONSTRUCTOR method of a (any) Z-class. Therefore, the constant DOCK_AT_TOP must be references as part of its class, i.e. CL_GUI_DOCKING_CONTAINER=>DOCK_AT_TOP.

Regards

Uwe

0 Kudos

i am new to oo abap, so kindly elaborate pls....

constructors are called by the system whenever an object instance to a class is created, so why are we needing this class reference ( side = cl_gui_docking_container => dock_at_top ) here ?

0 Kudos

cl_gui_docking_container => dock_at_top is a constant. It has a value. It doesn't matter what the value is, but lets say that, for the sake of example, it's 3. Now, which is more meaningful?

create object g_dock       " this is a constructor
  exporting
     side = 3

or

create object g_dock       " this is a constructor
  exporting
     side = cl_gui_docking_container=>dock_at_top

0 Kudos

This is beacuse your object reference g_dock will be instantiated (in client program) only AFTER exection of CREATE OBJECT statement, not during this statement. So later on you will indeed be able to access this constant the way you are trying, but BEFORE that this is not possible as object reference at that time is "pointing" to nothing. That's why you need to use class name in order to address constants.

One more reason and I think good practise is that all static or constant attributes should be accessed in this manner. This uniform notation provides a clear view for someone who doesn't know your code what are you in fact addressing. By using object reference it is ambigous whether underneath you access an instance/static/constant one.

Regards

Marcin

0 Kudos

Because it is a must have property for the instance. However it could be optional since you give a default value for the property.

0 Kudos

THNK U ALL..

pls clear this doubt..

when i create the object instance for the very first time..the constant appears as 'dock_at_left' by default.

data: g_dock type ref to cl_gui_docking_container,
        g_split type ref to cl_gui_easy_splitting_container,
        g_cont type ref to cl_gui_container.
 
create object g_dock       " this is a constructor
  exporting
     *side = dock_at_left*     ..."this comes as default
extension = 200.

but since i know that its a constant i write it as

side = cl_gui_docking_container=>dock_at_top

but since i am new to oo abap, i am unfamiliar to most of the class and its components, then how will i come to know dat where the coding will get changed similarly .

0 Kudos

but since i am new to oo abap, i am unfamiliar to most of the class and its components, then how will i come to know dat where the coding will get changed similarly .

- by reading papers, articles, blogs

- by searching the forum

- by reviewing sources of SAP tutorial programs

- by reviewing different classes

- by gaining experience

Regards

Marcin

0 Kudos

Hi Akash,

The replies to your question are good, in case you are not clear, let me try my bit

1.) we are already creating an instance of this class as 'g_dock'.

so instead of writing 'g_dock -> dock_at_top', why are we using ' clgui_docking_container=>dock_at_top_ '. pls explain this.

You mean to say something like this,


data: g_dock type ref to cl_gui_docking_container,
        g_split type ref to cl_gui_easy_splitting_container,
        g_cont type ref to cl_gui_container.
 
create object g_dock       " this is a constructor
  exporting
     *side = g_dock=>dock_at_top*      ..... "pls explain this syntax in general
extension = 200.

Well, as Marcin said object G_DOCK is not yet created at this point, so it is not possible to get a reference to any of it's attributes(constant/static/instance), when you try to compile the above code it will throw a compile error that g_dock=>dock_at_top is not known. Now if your question is how "cl_gui_docking_container=>dock_at_top" is valid syntax, the compiler checks if the global class "cl_gui_docking_container" exists and if yes, then checks for a constant attribute "DOCK_AT_TOP" within it.

2.) pls clarify 1 thing ....the syntax ...

"create object g_dock

exporting

......... "

it is a constructor method, so why are we writing 'cl_gui_docking_container=>dock_at_top' instead of writing only 'dock_at_top'

Again, "DOCK_AT_TOP" doesn't have any existence on its own, an attribute irrespective of whether it is constant/static/instance...will always be accessed via it's class reference, and again you cannot use g_dock until it's instance has been created already, not while creating it's instance.

Not sure if i understood your doubt in the above post, the parameter SIDE of the constructor let's you choose where do you want the container to show up top, left, bottom etc...by default it will be left, so if you do not pass anything to the parameter side, it will be created on the left. Now if you want it to be in any other place other than left then you have to specify the same by passing the corresponding value.

@TREASURE - My understanding is that the below is incorrect, and would hold good only within the class and not outside of it.

You could just have

DOCK_AT_TOP

Regards,

Chen

Edited by: Chen K V on Jun 6, 2011 4:54 PM

Edited by: Chen K V on Jun 6, 2011 4:56 PM

Former Member
0 Kudos

Clarification,

By saying

CREATE OBJECT <object_name>
                   EXPORTING 
                       <par1> = <val>
                       <par2> = <val>

What we are doing here is instanciate an object and send the constructor values that it is expecting on the constructor's import section, if we did not have paramaters expected on the constructor of class

cl_gui_docking_container

,

we would just say

CREATE OBJECT <object_name>

*as for *

cl_gui_docking_container=>dock_at_top

You could just have

DOCK_AT_TOP

alone if you want, but, yes there is a but!

It will come back to bite you, why,

class components are defined in terms of a given class

and most probably donu2019t have a lot of meaning outside of their defining classu2019s context. To avoid this kind of confusion, it is recommended that you always address class components by applying the class component selector to the defining

classu2019s name. Therefore,

cl_gui_docking_container=>dock_at_top

instead of just

dock_at_top

That way, your intentions are always clear.

I hope this makes sence.