Application Development and Automation 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: 
Read only

static Constructors and instance Constructors

Former Member
0 Likes
1,338

hi all,

where should we use static Constructors and where should we use instance Constructors .

can any one help me with the proper scenarios.

<removed by moderator>

thanks and best regards,

sravan.

Edited by: Mike Pokraka on Aug 3, 2008 6:04 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,022

Hii!

Instance constructor is a special instance method in a class and is always named CONSTRUCTOR.

These constructors are necessary after the instanctiation of class:

  • If you need to allocate the resources

  • You need to initialize attributes that cannot be covered by the VALUE addition to the DATA statement.

  • You need to send messages containing the information that a new object was created.

Static constructor is called automatically before the class is first accessed. This means that you can use this constructor if you want some values just before the class is first accessed.

If anyone has better answer. Please come forward.

Even I want to know more about static constructors

Regards

Abhijeet

8 REPLIES 8
Read only

Former Member
0 Likes
1,022

In this Forum it is Discussed many times, use Search Option.

and Before posting a Question, Read the rules .

Check this:-

http://help.sap.com/saphelp_nw04/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm

Regards

Vijay Babu Dudla

Read only

0 Likes
1,022

hi vijay,

i need functinal scenarios . means... example requirement at which we define these two constuctors.

thanks & Regards,

sravan.

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,022

Hello Sravan

The same questions were discussed recently:

Regards

Uwe

Read only

Former Member
0 Likes
1,022

This message was moderated.

Read only

Former Member
0 Likes
1,023

Hii!

Instance constructor is a special instance method in a class and is always named CONSTRUCTOR.

These constructors are necessary after the instanctiation of class:

  • If you need to allocate the resources

  • You need to initialize attributes that cannot be covered by the VALUE addition to the DATA statement.

  • You need to send messages containing the information that a new object was created.

Static constructor is called automatically before the class is first accessed. This means that you can use this constructor if you want some values just before the class is first accessed.

If anyone has better answer. Please come forward.

Even I want to know more about static constructors

Regards

Abhijeet

Read only

matt
Active Contributor
0 Likes
1,022

Static constructors in ABAP are called Class Constructors. I use them when I want to initialise some static attributes for later use.

For example, if I was writing a class to handle materials, I might define an internal table as a static attribute to hold of MAKT. Then when I wanted to get a material description, I'd go to the static attribute and read that, but first, I'd have to check whether it was populated.

IF sth_makt IS INITIAL.
  SELECT * FROM makt INTO TABLE sth_makt WHERE spras = sy-langu.
ENDIF.
READ TABLE sth_makt ASSIGNING <ls_makt> WITH TABLE KEY matnr = i_matnr.
...

And I'd have to do this everywhere I want to read sth_makt, and their could be a risk if someone is extending my class, or I'm enhancing it later, that I forget to check whether the table is filled.

If I put the select in the Class constructor, I don't have to worry about any check, safe in the knowledge that whenever I want to READ makt, it will already be populated.

matt

Read only

0 Likes
1,022

Thanks alot Matthew!