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

Constructors

Former Member
0 Likes
766

Hi,

I am not very clear with the Constructor Concept.Please brief me about the Static & Instance Constructors.

Thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
721
5 REPLIES 5
Read only

Former Member
0 Likes
722
Read only

Former Member
0 Likes
721

hi sneha,

<b>Constructor method</b>

Implicitly, each class has an instance constructor method with the reserved name constructor and a static constructor method with the reserved name class_constructor.

The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT statement, while the class constructor is executed exactly once before you first access a class.

The constructors are always present. However, to implement a constructor you must declare it explicitly with the METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no parameters.

<b>Static constructor</b>

The static constructor is always called CLASS_CONSTRUCTER, and is called autmatically before the clas is first accessed, that is before any of the following actions are executed:

Creating an instance using CREATE_OBJECT

Adressing a static attribute using <classname>-><attrbute>

Calling a ststic attribute using CALL METHOD

Registering a static event handler

Registering an evetm handler method for a static event

The static constructor cannot be called explicitly.

<b>Hope this is helpful, Do reward points.</b>

Read only

Former Member
0 Likes
721

Hi,

Refer

http://java.sun.com/docs/books/tutorial/java/TOC.html

Assume that all the three classes have their own static and Instance constructors.

Case 1. When class A is instantiated.

First the Static constructor of A will be called, then the Instance Constructor.

Case 2. When class B is instantiated

First the Static constructor of the Class B will be called, then the Instance Constructor. One has to explicitly call the construcotrs of class A in the contructors of class B. If constructors of class A are not called (Constructor of Super class is called by SUPER->Constructor in the sub-class constrcutor) in the sub class, then they are not executed. If they are called, then Static constructor of class A will be called first, then Instance Constructor.

Case 3. Whne Class C is instantiated.

Same as Case 2.

Regards

Read only

Former Member
0 Likes
721

HI

The constructor is a special instance method in a class with the name constructor.

Each class can have one constructor.

The constructor is automatically called at runtime within the CREATE OBJECT statement.

If you need to implement the constructor, then you must define and implement it in the PUBLIC SECTION.

You cannot normally call the constructor explicitly

<b>Static Constructor</b>

The static constructor is a special static method in a class with the name class_constructor. It is executed precisely once per program.

The static constructor of a class <classname> is called automatically when the class is first accessed, but before any of the following actions are executed:

Creating an instance in the class using CREATE OBJECT <obj>, where <obj> has the data type REF TO <classname>

Addressing a static attribute using <classname>=><attribute>

Calling a static attribute using CALL METHOD <classname>=><classmethod>

Registering a static event handler method using SET HANDLER <classname>=><handler_method> FOR <obj>

Registering an event handler method for a static event in class <classname>.

The static constructor cannot be called explicitly.

In the case of redefined methods, changing the interface (overloading) is not permitted; exception: Overloading is possible with the constructor.

Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract) subclasses, for example to define a uniform interface.

Classes with at least one abstract method are themselves abstract.

Static methods and constructors cannot be abstract.

You can specify the class of the instance to be created explicitly: CREATE OBJECT <RefToAbstractClass> TYPE <NonAbstractSubclassName>.

If it is to be impossible to instantiate a class more than once (for example, because it serves as a data administrator or data container), you can use the singleton concept. The class is defined with the addition CREATE PRIVATE and FINAL and instantiated using its static constructor.

<b>Reward if usefull</b>

Read only

Former Member
0 Likes
721

Thanks for the prompt reply,it resolved my doubt.