?
Solved

How to Construct a Java Object?

Posted on 2006-05-25
6
Medium Priority
?
220 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:_Esam
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 300 total points
ID: 16762477
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 16762506

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
 

Author Comment

by:_Esam
ID: 16762622
>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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Assisted Solution

by:appxpete
appxpete earned 200 total points
ID: 16762896
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
 
LVL 2

Expert Comment

by:appxpete
ID: 16762912
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
 
LVL 2

Expert Comment

by:appxpete
ID: 16762933
Oops again,

Type=Typo
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question