How to Construct a Java Object?

Hi,
I will read a String and pass it to one of the constructor.
In the second constructor, I would like to build the object...
I think I am not mistaken something...

Here is what I want to do:

public class EmailBehavior {
      // this is the line that will be passed to the constructor
      private String mEmailRecord = null;

               //these are the instance variables
      private String mEmailAddress = null;

      private String mRecordTypeCode = null;

      private String mSubCode = null;

      private String mEmailAddressNew = null;

      private String mDateTime = null;
      
      
      //default
        public EmailBehavior()
          {
          }  
      
                 //I would like to pass the String here and call the second constructor where I extract the substrings and assign the values to the instance variables......need help here
        public EmailBehavior(String pDataLine)
          {
                        //call to the second constructor
              this (pDataLine,?????????);
          }  // constructor



          /**
         *    <p>Constructor.</p>
         *  
         *  @param  pEmailAddress
         *  @param  pRecordTypeCode
         *  @param  pSubode
         *  @param  pEmailAddressNew
         *  @param  pDateTime
         */
        public EmailBehavior (String pEmailAddress, String pRecordTypeCode, String pSubode, String pEmailAddressNew, String pDateTime)
        {
          
          mEmailRecord = pDataLine.trim(); // how do I get the whole string and then substring it here and construc the object???
              mEmailAddress =
                    mEmailRecord.substring
                    (0,63).trim();
            mRecordTypeCode =
                mEmailRecord.substring
                    (64,65).trim();
            mSubCode =
                mEmailRecord.substring
                    (66,79).trim();
            mEmailAddressNew =
                mEmailRecord.substring
                    (80,207).trim();
            mDateTime =
                mEmailRecord.substring
                    (208,245).trim();
           
          
        }

        
//I would like to use this method to initialize them anyway....

        private void initializeObject()
          {
                    mEmailRecord              = null;
                mEmailAddress        = null;
                mRecordTypeCode = null;
                mSubCode                    = null;
                mEmailAddressNew          = null;
                mDateTime             = null;
          }  // method initializeObject


      

}

Thanks..
_Esam
_EsamAsked:
Who is Participating?
 
WebstormConnect With a Mentor Commented:
Hi _Esam,

If you want to call a second constructor from another one, you'll have to do this in the first line only.


Another solution is to create a private method to build your object :

public EmailBehavior(String pDataLine)
         {
             ... split parameters from pDataLine
             //call to the second constructor
             build ( ....);
         }

public EmailBehavior (String pEmailAddress, String pRecordTypeCode, String pSubode, String pEmailAddressNew, String pDateTime)
{ build(pEmailAddress, pRecordTypeCode, pSubode, pEmailAddressNew, pDateTime); }

private build (String pEmailAddress, String pRecordTypeCode, String pSubode, String pEmailAddressNew, String pDateTime) {...}
0
 
WebstormCommented:

public EmailBehavior(String pDataLine)
{
         pDataLine= pDataLine.trim();
         String mEmailAddress = pDataLine.substring(0,63).trim(),
                   mRecordTypeCode = pDataLine.substring(64,65).trim(),
                   mSubCode =  pDataLine.substring(66,79).trim(),
                   mEmailAddressNew = pDataLine.substring(80,207).trim(),
                   mDateTime = pDataLine.substring(208,245).trim();
         build(mEmailAddress ,mRecordTypeCode ,mSubCode ,mEmailAddressNew ,mDateTime );
}
0
 
_EsamAuthor Commented:
>public EmailBehavior(String pDataLine)
         {
             ... split parameters from pDataLine
             //call to the second constructor
             build ( ....);
         }


Here I call the second constructor! Do I need to call the build (method) too?
What do I do in the build method???


public EmailBehavior(String pDataLine)
{
         pDataLine= pDataLine.trim();
         String mEmailAddress = pDataLine.substring(0,63).trim(),
                   mRecordTypeCode = pDataLine.substring(64,65).trim(),
                   mSubCode =  pDataLine.substring(66,79).trim(),
                   mEmailAddressNew = pDataLine.substring(80,207).trim(),
                   mDateTime = pDataLine.substring(208,245).trim();
         build(mEmailAddress ,mRecordTypeCode ,mSubCode ,mEmailAddressNew ,mDateTime );
}

private build (String pEmailAddress, String pRecordTypeCode, String pSubode, String pEmailAddressNew, String pDateTime) {
mEmailAddress           = pEmailAddress
mRecordTypeCode    = pRecordTypeCode
 mSubCode                       = pSubode
mEmailAddressNew             = pEmailAddressNew
mDateTime  = pDateTime

}

DO I NEED TO HAVE A SECOND CONSTRUCTOR WITH ALL THESE PARAMETERS?



Thax..

_Esam..
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
appxpeteConnect With a Mentor Commented:
Will this work?  It sounds like you only really want to call the one constructor giving it the long string that has the data aligned at certain offsets within the string.


public class EmailBehavior
{
    //these are the instance variables
    private String mEmailAddress;
    private String mRecordTypeCode;
    private String mSubCode;
    private String mEmailAddressNew;
    private String mDateTime;
     
    //default
    public EmailBehavior()
    {
        initializeObject();
    }
 
    public EmailBehavior(String pDataLine)
    {
        this ();
        setEmailData(String pDataLine)
    }

    public void setEmailData(String pDataLine)
    {
        mEmailAddress = pDataLine.substring(0,63).trim();
        mRecordTypeCode = pDataLine.substring(64,65).trim();
        mSubCode = pDataLine.substring(66,79).trim();
        mEmailAddressNew = pDataLine.substring(80,207).trim();
        mDateTime = mEmailRecord.substring(208,245).trim();
    }

    private void initializeObject()
    {
        mEmailRecord = null;
        mEmailAddress = null;
        mRecordTypeCode = null;
        mSubCode = null;
        mEmailAddressNew = null;
        mDateTime = null;
    }
}
0
 
appxpeteCommented:
Oops,

Type in this method:

    public void setEmailData(String pDataLine)
    {
        mEmailAddress = pDataLine.substring(0,63).trim();
        mRecordTypeCode = pDataLine.substring(64,65).trim();
        mSubCode = pDataLine.substring(66,79).trim();
        mEmailAddressNew = pDataLine.substring(80,207).trim();
        mDateTime = pDataLine.substring(208,245).trim();
    }
0
 
appxpeteCommented:
Oops again,

Type=Typo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.