Skip to Content
author's profile photo Former Member
Former Member

Instantiation of class only when required parameters are present


I have a class which has user id as import parameter for the constructor. My class is instantiated based on the USER ID and populates the attributes PERNR, NAME and POSITON.

Now when the constructor method is executed my class gets instantiated in all the scenarios. When i dont pass an user id all my parameters are blank but my class still creates an instance. But i want my instance to be created only when a valid user id is passed.

I tried to exit the constructor when an invalid user id is passed but still my class gets instantiated (with all attributes blank). One way could be i could check for the validity of the user id before instantiating my class but i want my constructor to take care of this.

Is this possible? Can someone let me know how to achieve this?


Prasath N

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Jun 13, 2008 at 08:08 AM

    you should create a global method FACTORY that will handle the creation of objects

    then in your program, you don't use CREATE OBJECT but in place

    my_new_object_ref = ZCL_MY_CLASS=>FACTORY( parameters ).

    This method FACTORY will do the CREATE OBJECT only if required parameters are present

    Then if you want to be sure the object is always insatnciated using the factory, you change attribute of class "instanciation" to "private" so that noone can create an object from outside the class

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 13, 2008 at 08:44 AM

    Hello Prasanth

    You need to raise an exception if the USER ID is not valid, e.g.:

    METHOD constructor. 
    " IMPORTING parameter id_userid
    " Check user id e.g. with BAPI_USER_EXISTENCE_CHECK and evaluate RETURN parameter
      IF ( ls_return-type CA 'AEX' ).
        MESSAGE 'User does not exist' TYPE 'E'
        RAISING not_exists.

    Either you define an exception or an exception class in the CONSTRUCTOR signature.



    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 06:41 AM


    This is easy. You may mark the parameter of the constructor as not optional. Check the parameter of the constructor. You will get the answer?



    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.