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

constructor

Former Member
0 Likes
2,404

Hi friends,

can any body tell me exactly

what is the difference between CONSTRUCTOR & METHOD of a class.?

points will be awarded

Regards,

kumar.

11 REPLIES 11
Read only

Former Member
0 Likes
1,822

Hi,

The constructor method is called automatically when you instantiate the class ( create object statement).
 You generally pass the parameters of the method in the create object statement.

It is also a method.

but the difference is that it is called automatically.

While the normal method is called when u explicitly call that method by class name or object name.

Best regards,

Brijesh

Read only

Former Member
0 Likes
1,822

Hi,

You can refer to the following SAP Dcoument for more clarity

http://help.sap.com/erp2005_ehp_03/helpdata/EN/08/d27c03b81011d194f60000e8353423/content.htm

Read only

Former Member
0 Likes
1,822

Hi satish,

Pls Go Through the Documentation In The Below Link ,you

will understand -

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm

http://saphelp.border-states.com/EN/c3/225b5c54f411d194a60000e8353423/frameset.htm

Hope It will help you.

Regards,

Sujit

Read only

Former Member
0 Likes
1,822

Hi,

CONSTRUCTOR is a method that is automatically called during object creation. Even when you do not define a CONSTRUCTOR in the class, their exists one called the default constructor. The constructor always has a returning parameter and it returns the object reference. Constructor is more like malloc in C where the memory is allocated to the object.

Method is the bheaviour of the object/class. It needs to be implicitly called. Methods can have returning/exporting parameters. Methods do not allocate memory.

I hope this clarifies your doubt.

Regards,

Saurabh

Read only

former_member787646
Contributor
0 Likes
1,822

Hi,

CONSTRUCTOR: It is a special method which is called

automatically by the runtime system (depends on the type)

when a new object is created OR the class is called for the first time .

METHOD: It describes the behaviour of an object. It contains

the functionality which an object can perform. It should be

called explicitly in the calling program unless it is a

CONSTRUCTOR OR CLASS_CONSTRUCTOR method.

Hope it helps you.

Murthy

Read only

Former Member
0 Likes
1,822

Hi,

Method -

Methods are the internal procedures of a class .

Have a parameter interface, through which the system passes values to them when they are called, and through which they can return values to the caller.

Can access all the attributes of their class.

The private attributes of a class can only be changed using methods.

One can draw the analogy between methods and function modules in ABAP

constructor -

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.

Reward if helpfull

Regards,

Naresh.

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
1,822

Hi,

You can see this three examples:

  • [ABAP Objects - Creating your First Local Class - Defining Components|https://wiki.sdn.sap.com/wiki/x/OKg] - Its demonstrates how to define and implement Methods.

  • [ABAP Objects - Creating your First Local Class - Using Instance Constructor|https://wiki.sdn.sap.com/wiki/x/0dg] - Its demonstrates how to define and implement Instance Constructor.

  • [ABAP Objects - Creating your First Local Class - Using Static Constructor|https://wiki.sdn.sap.com/wiki/x/ZtM] - Its demonstrates how to define and implement Static Constructor.

You can Cope & Past this examples and run in your own

I hope this examples and all other users of explanations be useful for you !

Greetings.

Marcelo Ramos

Read only

Former Member
0 Likes
1,822

Hi,

Constructor is a special method of object which is used to initialize the attributes of object at the time of creation.Constructor cannot be called explicitly.Method is called explicitly which is used to perform an action to the object.

Thanks,

Naveen Kumar.

Read only

Former Member
0 Likes
1,822

Hiii!

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

The constructor is automatically called at runtime with CREATE OBJECT statement.

Some Important points about constructor:

Each class can have only one constructor.

The constructor must be defined in PUBLIC SECTION.

The constructor's signature can have only importing parameters and exceptions.

When exceptions are raised in the constructor, instances are not created so no main memory is occupied.

Static Constructor = This is a special static method in a class and is always names CLASS_CONSTRUCTOR. It is executed once per program. This constructor is called automatically before the class is first accessed, but before any of the following actions are executed for the first time:

Creating instance of this class(CREATE OBJECT)

Accessing a static attribute of this class.

Calling a static method of this class.

Registering an event handler method for an event in this class.

Some important points:

Each class has only one static constructor

This constructor must be defined in PUBLIC SECTION.

The constructor's signature cannot have importing parameters or exceptions.

The static constructor cannot be called explicitly

Regards

Abhijeet Kulshreshtha

Read only

Former Member
0 Likes
1,822

Hiii!

Methods are internal procedures in classes that determine the behaviour of the objects. they can access all attributes in their class and can therefore change the state of other elements.

Methods have a signature that enables them to receive values WHEN THEY ARE CALLED and pass values back to the calling program.Methods can have any number of importing, exporting and changing parameters.

Constructors are basically a method which are called automatically at runtime with the CREATE OBJECT statement.

CONSTRUCTORS CAN ONLY BE DECLARED IN PUBLIC SECTION, WHILE METHODS CAN BE DECLARED BOTH IN PRIVATE AND PUBLIC SECTION.

Just check out this link, all your doubts regarding ABAP Objects will be cleared

http://www.sap-press.de/katalog/buecher/htmlleseproben/gp/htmlprobID-28?GalileoSession=22448306A3l7U...

Regards

Abhijeet

Edited by: Abhijeet Kulshreshtha on Jul 11, 2008 8:50 AM

Read only

former_member8532
Participant
0 Likes
1,822

Constructor: Constructor is related to object creation it will be called only when your create object statement executed .its use it to set the state of object ,because each object has its own copy of attribute like you have defined variable like data: roll. no. type i, each object has its own copy of roll. no.

so threw constructor you can set object attribute value.its same like method but called by object only and once when a new object created. there is default constructor within every class.

it you do not write your constructor default constructor called .

you can call your constructor threw object .

Method: here u defined the functionality that your object want ,

suppose you want add some value on this roll. no.or you want to write

some o/p you can just write:/ statement here.

it can be called many times on the same object or different .

but it can be called threw object. and there is no default method.