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

private constructor in abap OO

Former Member
0 Likes
3,788

Hi folks,

I'd like to create a factory method in a class. The factory method returns objects (corresponding to certain keys) of the class by first checking an internal map for the key and then returning the pertaining object if an entry for that key exists or creating a new object (and putting it into the table) if it doesn't.

Now I wanted to declare the actual constructor <i>private</i>, but found SE80 prohibits changing the accessibility of constructors - there are always public?!

Is there a way to achieve private accessibility of constructors, what does <i>Instantiation - private</i> on the properties tab of a class mean (as opposed to possibly changing the constructor to private)?

Great if someone could shed some light onto this.

Regards,

Sebastian

1 ACCEPTED SOLUTION
Read only

ChristianFi
Active Participant
0 Likes
1,850

since you can not call the constructor directly there is no need to declare the constructor as private.

To avoid that an instance can be created from outside the class jsut declare it as

class lcl_class definition create private.

or if it is global:

go to tab properties and set choose private in the instantiation.

sorry just realised that you were asking exactly that question.

With create private only methods of this class can create an instance of this class.

create proteced extends it to inherited classes and public means that every method can create instances.

Christian

Message was edited by: Christian Finkbeiner

4 REPLIES 4
Read only

ChristianFi
Active Participant
0 Likes
1,851

since you can not call the constructor directly there is no need to declare the constructor as private.

To avoid that an instance can be created from outside the class jsut declare it as

class lcl_class definition create private.

or if it is global:

go to tab properties and set choose private in the instantiation.

sorry just realised that you were asking exactly that question.

With create private only methods of this class can create an instance of this class.

create proteced extends it to inherited classes and public means that every method can create instances.

Christian

Message was edited by: Christian Finkbeiner

Read only

0 Likes
1,850

> since you can not call the constructor directly there

> is no need to declare the constructor as private.

well, that is almost a philosophical question. I mean in a way I do call the constructor directly: it is called immediately after using the <i>create object</i> command.. but I know what you mean

> To avoid that an instance can be created from outside

> the class jsut declare it as

> class lcl_class definition create private.

>

> or if it is global:

> go to tab properties and set choose private in the

> instantiation.

> ...

> With create private only methods of this class can

> create an instance of this class...

That is, only methods of this class can call the constructor (use <i>create object</i>), right?

This would indeed have the desired effect that creation of this class' objects (somewhere outside the class) was only possible through the factory and not by calling the constructor.

Thanks, Sebastian

Read only

0 Likes
1,850

I just tried to reward points for Christian's posting by marking the <i>problem solved</i> radiobutton, but I am constantly getting a "Rewarding the message failed."-error. Has someone an idea what could be wrong?

Read only

0 Likes
1,850

Yep you got me right and the points are assigned. Thank you

Christian