cancel
Showing results for 
Search instead for 
Did you mean: 

Please give me answer

Former Member
0 Kudos

Hi all,

What is the difference between

TYPE REF TO and

TYPE STANDARD TABLE OF

Thanks

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi

- TYPE REF TO is used to declare a variable refered to certain class, so you use it if you want to program using OO.

DATA: MY_OBJECT TYPE REF TO <CLASS>.

- TYPE STANDARD TABLE OF is used to declare a internal table, this statamen is like:

BEGIN OF .... OCCURS n,

END OF ....

But it creates a table without header line, if you want the header line use WITH HEADER LINE options:

DATA: BEGIN OF T_BKPF OCCURS 0.

INCLUDE STRUCTURE BKPF.

END OF T_BKPF.

DATA: T_BKPF TYPE STANDARD TABLE OF BKPF WITH HEADER LINE.

These two stataments are similar.

Max

Former Member
0 Kudos

Hi Brunda,

<b>TYPES - REF TO</b>

Syntax

TYPES dtype { {TYPE REF TO type}

| {LIKE REF TO dobj} }.

Effect

The addition REF TO specifies a data type for a reference variable. The entry behind REF TO specifies the static type of the reference variable. The static type restricts the object quantity to which a reference variable can refer. The dynamic type of a reference variable is the data type and the object class to which it refers. The static type is always more general or the same as the dynamic type (see also Assignments Between Reference Variables).

You can use the TYPE addition to define data types for data and object reference variables. You can only define data types for data reference variables with the LIKE addition.

Data References

If, for type, you have specified the predefined generic data type data, the system creates a data type for a data reference variable from the static type data. Such reference variables can refer to any data objects but can only be dereferenced in the statement ASSIGN.

If, for type, you have specified any non-generic data type (a non-generic data type from the ABAP Dictionary), a non-generic program-local type already defined with TYPES, or a non-generic predefined type (d, f, i, string, t, xstring), the system creates a data type for a data reference variable with the relevant static type. Such reference variables can refer to all data objects of the same type and can be dereferenced to matching operand positions using the dereferencing operator ->*.

If, for dobj, you have specified a data object of the same program, the system creates a data type for a data reference variable whose static type is adopted from the data type of the data object. Such reference variables can refer to all data objects of the same type and can be dereferenced to matching operand positions using the dereferencing operator ->*. Within a procedure, you cannot specify a generic typed formal parameter for dobj.

Object References

If, for type, you have specified a global or local class, the system creates a data type for a class reference variable whose static type is the specified class. Such reference variables can refer to all instances of the class and its subclasses. You can access all components of an object with a class reference. If the static type is the super class of the dynamic type, then the components that can be statically called are a subset of the components of the dynamic type. You can call all attributes and methods of a dynamic type using a class references and the dynamic object attribute call (see ASSIGN) and the dynamic method call (see CALL METHOD).

If, for type, you have specified a global or local interface, the system creates a date type for an interface reference variable whose static type is the specified class. Such reference variables can refer to objects of all classes that implement the interface. When calling components of objects with interface references, the name of the static type is always implicitly placed before the component. With an interface reference, you can only access the interface components of an object that are known in the static type. This applies for the dynamic and the static call.

Example

This example shows how data types are defined for an interface reference and for a class reference as well as a data reference to an interface reference.

INTERFACE i1.

...

ENDINTERFACE.

CLASS c1 DEFINITION.

PUBLIC SECTION.

INTERFACES i1.

ENDCLASS.

TYPES: iref TYPE REF TO i1,

cref TYPE REF TO c1,

dref TYPE REF TO iref.

And ,

<b>TYPE STANDARD TABLE OF xxxx WITH HEADER LINE</b> is used to define an internal table in which each row is of the structure xxxx:

Regards,

Raj

Former Member
0 Kudos

Hi Brunda,

1. Though both are defined using TYPE,

the main difference is :

Ref to : It REFERS to (Points to something)

Standard Table of : True variable/data having

its own memory space.

2. eg

DATA : cont TYPE REF TO cl_gui_custom_container.

data : myitab type standard table of t001.

3. cont is just like a POINTER.

myitab is a variable/internal table

having its own memory space.

regards,

amit m.

former_member188685
Active Contributor
0 Kudos

TYPE REF TO is used ABAP Objects to create the instance of a class.

data: o_obj type ref to cl_gui_alv_grid.

type standarad table of is used to create the internal table .

data: itab type standard table of mara.

Former Member
0 Kudos

Hi,

Type ref to is used to create the instance of a class where as type table of is used to define the table of another one.

rgds

p.kp

Former Member
0 Kudos

Thanks Krishna

Can TYPE REF TO be used other than in creating the instance of a class.( I mean other than ABAP objects)

former_member188685
Active Contributor
0 Kudos

No, it is only to cerate the instance of the class.

vijay

Former Member
0 Kudos

Hi all,

Thanks for replying.

Can TYPE REF TO be used to refer internal table or stucture or any database table.

something like this

DATA: I_TAB TYPE REF TO I_STRU.

former_member188685
Active Contributor
0 Kudos

Hi,

You can use.

data: itab type ref to mara.

try that in report.

thanks

vijay

Former Member
0 Kudos

Hi Brunda,

Let me make a point clear here. TYPE STANDARD TABLE OF is used for internal tables. By definition when you have TYPE in DATA this creates a variable while it executes your progam but when you use TYPE REF TO it only creates a reference variable.

The difference between them is variable is a area where value can be stored while the program executes but a reference variiable is one which holds the address of a data object created. For example, instance of a class is created and the address of the instance is stored in a reference variable. when you access any attributes it actually refers the instance and gets the value.

<b> We can use TYPE REF TO also for a generic data type called data and also for a generic instance reference called object.</b> If you see there is neither a datatype called data or a class called object. Here when you say

DATA l_ref TYPE REF TO data.

CREATE DATA l_ref TYPE <something>

It creates a variable or an object of this type and l_ref just points to it. Hope I answered your query.

Regards,

Srikanth

Former Member
0 Kudos

Thanks Vijay,

Exactly what I want.....but I want to know the meaning to this

DATA:ITAB TYPE REF TO MARA.

Former Member
0 Kudos

Hi Brunda,

1. using that way, itab will act like a POINTER.

It becomes OBJECT ORIENTED

2. If we further use this statement :

write 😕 itab-matnr.

it will give error.

3. if we use :

write 😕 itab->matnr.

it will work.

NOTICE THE SYNTAX ->

regards,

amit m.

former_member188685
Active Contributor
0 Kudos

data: itab type ref to mara.

if you want to access the elements

you need to do some thing like this

itab->matnr

itab->matkl..

check it.

vijay

former_member188685
Active Contributor
0 Kudos

small example...

data: itab type ref to mara.
select  matnr
       up to 1 rows
       from mara
       into itab->matnr.
       
  endselect.
write: itab->matnr.

thanks

vijay

Former Member
0 Kudos

Thank Amit and Vijay,

Now I understood and solved my problem

Thank you very much