‎2008 May 16 12:47 PM
hai friends.....
can anybody tell me about constructors .when wil be the exact use of instance and static methods and attributs will be occursd..
plz tell me exact real scanario where it occurs /////////
thankx in advance...
‎2008 May 16 1:04 PM
Hello,
Please read these: [http://help.sap.com/saphelp_nw2004s/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm] and
[http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6554f411d194a60000e8353423/content.htm].
Regards,
‎2008 May 16 1:27 PM
Hi Satya Vani,
Constructors are special PUBLIC methods that are triggered when an object is instantiated from a class. They are necessary when you want to set the initial state of an object dynamically.
Like normal methods, there are two types of constructor - instance constructors and static constructors.
To use the instance constructor, the CONSTRUCTOR method must be declared in the public section of the class using the METHODS statement, and implemented in the implementation section.
Constructors must always be declared in the PUBLIC section of a class.
Static Constructor
REPORT YSUBOOPS2.
CLASS c1 DEFINITION .
PUBLIC SECTION.
CLASS-DATA : NUM TYPE I VALUE 5.
CLASS-METHODS:CLASS_CONSTRUCTOR.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE:/5 'I am class constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
WRITE:/5 C1=>NUM.
Static methods,
Static methods declared as CLASS-METHODS : CLASS_CONSTRUCTOR in the public section of the class definition and are also implemented in the implementation part.
Call a static method : [CALL METHOD] class=>meth.
Addressing a static attribute with class=>a.
Simple program will show you that instance constructor methods of a class get triggered when an object is created from the class.
REPORT YSUBOOPS1.
CLASS C1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
WRITE:/5 'I am constructor'.
skip 2.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: obj1 TYPE REF TO c1.
CREATE OBJECT: obj1.
Best Regards
Suresh
‎2008 May 18 6:12 PM
>
> Hi Satya Vani,
>
> Constructors are special PUBLIC methods that are triggered when an object is instantiated from a class. They are necessary when you want to set the initial state of an object dynamically.
> Suresh
Not quite
DATA: lo_obj TYPE REF myclass.
CREATE OBJECT lo_obj. " EXPORTING parameters may follow.When the above is executed the constructor of the class myclass is run. If no method CONSTRUCTOR is defined, then a reference to an object of type myclass is returned. In such cases, there are no parameters. You can, however, define in the class a method called CONSTRUCTOR (with none or many importing parameters). If such a method exists, then the create object command will run the constructor method and return an instance of the class.
It is possible, however, that instantiation is private. That means that the CONSTRUCTOR is private. In such instances the return of new references to a caller is handled through what is know as a factory class. This is a static class that has within its implementation a CREATE OBJECT of myclass. It returns the reference to the object. Used when implementing the singleton pattern and various other scenarios.
‎2008 May 16 2:41 PM
Hi Satya,
Take a look at this link [ABAP Objects - Creating your First Local Class - Using Instance Constructor|https://wiki.sdn.sap.com/wiki/x/0dg], it's explaining how to use Instance Contructor.
Kind Regards,
Marcelo Ramos