Not getting the use of 'this' keyword in constructor as shortcut approach.

class Box{
int width;
int height;
int depth;
Box(){ //no-arg constructor
Width =1;
Height = 2;
Depth = 3;
Box(int width){
this.width = width;
height = 2;
depth = 3;
Box(int width, int height){
this.width = width;
this.height = height;
depth = 3;
/*Box(int width, int height, int depth){
this.width = width;
this.height = height;
this.depth = depth;
*//short cut approach*
*Box(int width, int height, int depth){*
this(width, height); //calls matching constructor from the same class
this.depth = depth;
Box(Box b){
this.width = b.width;
this.height = b.height;
this.depth = b.depth;
public class ConstructorDemo {
public static void main(String[] args) {
Box b1 = new Box(1,2,3);
Box b2 = new Box(b1);
In the above example, 'this' pointing to current object is understood but
this(width, height); is used instead of this.width=width; this.height=height;
how can it be shortcut when we have declared constructors ? And really how its so useful in a big code?
Edited by: 1010533 on Jun 7, 2013 12:54 PM

Welcome to the forum!
>
class Box{
int width;
int height;
int depth;
Box(){ //no-arg constructor
Width =1;
Height = 2;
Depth = 3;
Box(int width){
this.width = width;
height = 2;
depth = 3;
Box(int width, int height){
this.width = width;
this.height = height;
depth = 3;
/*Box(int width, int height, int depth){
this.width = width;
this.height = height;
this.depth = depth;
//short cut approach
Box(int width, int height, int depth){
this(width, height); //calls matching constructor from the same class
this.depth = depth;
Box(Box b){
this.width = b.width;
this.height = b.height;
this.depth = b.depth;
public class ConstructorDemo {
public static void main(String[] args) {
Box b1 = new Box(1,2,3);
Box b2 = new Box(b1);
In the above example, 'this' pointing to current object is understood but
this(width, height); is used instead of this.width=width; this.height=height;
how can it be shortcut when we have declared constructors ?
>
Shortcut? Who said it is a shortcut?
>
And really how its so useful in a big code?
>
EXTREMELY useful - especially in 'big code' (whatever you mean by that).
Constructors are meant to create VALID instances. The business rules for creating a valid instance may not be as simple as your example.
Ideally code should be written once and used often. The use of constructors is no different.
Once you have a contructor for a given set of arguments you should use it if you need to construct an instance like that.
So when you need a constructor that uses those same arguments and then some additional ones your new constructor should call the existing constructor and should then apply the new logic needed for the new constructor.
That is important because it means that the business logic needed for those two arguments is only implemented in ONE place. That logic might be very complicated and there is no valid reason to duplicate it.
Once you have a piece of code that works reuse it whenever possible.

Similar Messages

  • DO NOT Get The Service With This Company IF You Want to Receive a Good Customer Service

    It is really a pain, dissatisfaction, and a lot more when you have Verizon as your service provider. I have been having service for my cellphones with Verizon for over 3 years, and did not have any major problems, so when I moved to my new place I thought it’s better to get the Fios, and have one bill for my all Verizon services. I went online and started to order my products online. During ordering, the chat session opened and I started chatting with the Customer Service Rep in order to get help through the process. When we were in the step that I could order the home phone plan, I saw the International World Plan, and I asked the lady to give me some information about this plan. I asked her that I want to call to my country and I mentioned my country’s name to her as well. I asked her that if this service is available for my country, and she said “Yes” you can call to over 100 countries with this service and she encouraged me to get the 300 minutes service. Since the plan sounded good to me I decided to get the 500 minutes instead of 300 min, and after I ordered my plan, I started calling overseas, and happy that I am paying less than the Phone cards with this plan. On March 26 I received my bill, and saw that Verizon charged me 142 $ for the less than 200 minutes that I called to my country, and there was a Letter “N” in front of each phone calls. I checked the guide to see what does “N” mean, and I saw that it said N means “Denoted calls are NOT listed in World Plan”. Well, thanks to the lady who gave me the wrong information while I was ordering my plan online, but she or the Verizon company is not that much lucky because I printed my chat session the day I ordered my plan since I wanted to have the information of what I actually had ordered. That time I did not think that I may get in trouble like this. Since March 27th, I have been calling Verizon every other day, and I have explained my issue to more than 10 Customer Service Reps, and each of them asked me to fax my chat session to them. I have been faxing that to all the numbers that they gave me, and I am keep faxing them, but do not get any respond. One of the Verizon’s Supervisors, whose name is Mark, called twice on my home phone number during the daytime, around 3 pm, and he left massages. On his second massage, he said that he checked the plan and my country is not listed in the International World Plan. He just said this, and he said that he was going to call me back, but never happened. He also did not leave me any phone number that I could call back. Also, since my cellphones are listed in the same bill as my other products, he could easily see the other numbers that he could call and reach me, but he did not bother himself to do so. I have even changed my home voice mail greeting addressing to Verizon, and left my cellphone number in voice mail greeting. I am so sorry that I have to say this, but this seems a definite fraud to me. I did not know about this plan, and I asked the Customer Service Rep to help me. She was responsible to check my country, or at least let me know how I could find out about the coverage. This is ridiculous, and if Verizon cannot take over this problem, my lawyer will take the action over, and takes the issue to their corporate court. I am not going to give up about this, and I decide to give Verizon about a week or so to solve this issue, but I won’t wait more than that. Just a friendly advice to people who want to get the home service through Verizon: DO NOT Get The Service With This Company IF You Want to Receive a Good Customer Service. There is nothing about taking care of customer in this company, and you are going to be stuck with what you get.      

    Mahsa21,
    We are glad that we were able to resolve the international calling plan issue for you.  If you need assistance,please  reach out to us.
    thanks,
    Tonya D.

  • I am using the last mac os. safari keeps giving me gibberish fonts on random webpages, but using other browsers and other OSs I DO NOT get the gibberish.  this issue was solved by Bee who directed the user to remove duplicate fonts... where's the fix?

    On the same OS, I also use Firefox but it does not happen with firefox.  Is there a fix ???

    Have you run Font Book to check for font conflicts?

  • TS2846 after reset settings of 3gs it asking countery and after thatchoose a network but it does not getting the signall in this step.pls help

    after reset settings it asking language after country then it does not catching signal & it also not conneted to i tunes. pls help

    Resolve iOS update and restore errors - Apple Support

  • About "this" keyword in constructor

    Hi im new to java... Actually what is the use of "this" keyword? I have tried reference books and internet site still no prevail. As i see there are times when the constructor as
    public hello(int number, number2){
    n = number;
    m = number2;
    without this and some are
    public hello(int number, number2){
    this.number = number;
    this.number2 = number2;
    im so confused please help

    It is a context reference keyword.
    Basically, it is a safety word that tags a specific variable to a method or class.
    In a class, you can have a member variable called aNumber. Now, in method, you could supply an argument that is also called aNumber. Example below:
    public class MyClassExample {
        int aNumber;
        public MyClassExample(int aNumber) {
            aNumber = 1;
    }In the above example, how does Java know what 'aNumber' I am refering to? The class member variable (global variable) or to the method argument variable (local variable)?
    The "this" keyword marks the context to which we refer. If we wanted to tell Java to use the value of the method's argument as the value for our class variable, we would do (this):
    public MyClassExample(int aNumber) {
        aNumber = this.aNumber;
    }Now, what does this really do? Well, Java transparently switches the context for us, but this time we define it.
    When we use the variable name, Java actually records the full context of "MyClassExample.aNumber" with the "MyClassExample.MyClassExample.aNumber"
    See, I bet you never knew that!
    What happens if we want it the other way around, then change which object to place the this keyword to.

  • TS3988 I just bought a Iphone 5 -- my Iphone 4 and my ipad used to automatically sinc calendars via the Icloud whenever I entered anything on either device -- I can not get the new phone to do this.I have  check the settings on both devices and they seem

    I just bought a Iphone 5 -- my Iphone 4 and my ipad used to automatically sinc calendars via the Icloud whenever I entered anything on either device -- I can not get the new phone to do this.I have  check the settings on both devices and they seem to be t

    There are only two things that you must do to connect a device to calendars:
    1)  In settings>icloud, you must log in, WITH THE CORRECT ID/PASSWORD, and
    2)  Turn on calendars on the same page.
    You said that the iphone 4 and ipad **used** to automatically sync.  Do they no long sync?

  • I am not getting emails - it says this iphone cannot be used because the apple mobile device is not started

    I am NOt getting email it says this iphone cannot be used because the Apple Mobile Device is not started

    This should help ya out -> How to restart the Apple Mobile Device Service (AMDS) on Windows

  • In iCal - I keep getting the error message " This calendar was created by Mail" and it will not accept any input; why..??

    In iCal - I keep getting the error message “ This calendar was created by Mail” and it will not accept any input; why..??

    use Disk Utility and Verify Permisions then fix and Verify Disk and fix, this should fix it.

  • Iam not getting the data on screen after using the user exit zxpadu01

    Dear Freinds
                As per my requirement when the user enters value Ansal  it should get defaulted to Amount field (bet01 ) based on the wage type
    i have written calculation ( q0008-bet01 = ansal/100) before it getting defaulted to bet01, i found that value is not getting default  on the screen for bet01 once i enter value for Ansal , please any body correct my code
    in   ZXPADU02 i have written the code as  below :
    data : wa_p0008 like p0008,
            i0008 like p0008,
           l_v_ANSAL type ANSAL_15,
           l_v_amount type PAD_AMT7S.
    CASE innnn-infty.
    when '0008'.
    CALL METHOD cl_hr_pnnnn_type_cast=>prelp_to_pnnnn
    EXPORTING
    prelp = innnn
    IMPORTING
    pnnnn = wa_p0008.   -- HERE I GOT ALL THE DATA EXCEPT BET01
    if wa_p0008-lga01 = 'MFPY'.
    l_v_ansal =  wa_p0008-ansal.
    l_v_amount = l_v_ansal / 100.
    move l_v_amount to wa_p0008-bet01.
    CALL METHOD cl_hr_pnnnn_type_cast=>pnnnn_to_prelp
    EXPORTING
    pnnnn = wa_p0008  --- HERE I CAN SEE EVEN THE BET01 IS FILLED
    IMPORTING
    prelp = innnn.         --- HERE I CAN SEE LGART BUT NOT AMOUNT VALUE
    endif.
    when others.
    endcase.
    IN ZXPADU01 I HAVE WRITTEN AS BELOW:
    data : wa_p0008 like p0008,
            i0008 like p0008,
           l_v_ANSAL type ANSAL_15,
           l_v_amount type PAD_AMT7S.
    CASE innnn-infty.
    when '0008'.
    MOVE-CORRESPONDING  innnn to wa_p0008.
    CALL METHOD cl_hr_pnnnn_type_cast=>prelp_to_pnnnn
    EXPORTING
    prelp = innnn
    IMPORTING
    pnnnn = wa_p0008.
    if wa_p0008-lga01 = 'MFPY'.
    l_v_ansal =  wa_p0008-ansal.
    l_v_amount = l_v_ansal / 100.
    move l_v_amount to wa_p0008-bet01.
    CALL METHOD cl_hr_pnnnn_type_cast=>pnnnn_to_prelp
    EXPORTING
    pnnnn = wa_p0008   --- I CAN SEE ONLY WHEN I COME AGAIN
    IMPORTING
    prelp = innnn.
    endif.
    when others.
    endcase.
    Now my problem is as follows :
                   i have entered the value for Ansal (ex: p0008-Ansal = 1000) ..........and i say enter then i should found the calculated value for the ansal through my coding
    and it should  display in bet01 as 10 ( q0008-bet01 =  p0008-ansal /100)
    but i dont find the value 10 being displayed for the field bet01 on the screen  when
    user enters Ansal  as 1000 .........even then i have saved it to test the scenario......
    now i came in displayed mode (pa20 ) for the infotype 008 for the same personnel no for the same  dates .....where i created the record........i found that the value
    bet01 is there i.e i can see  the value bet01 as 10 ( my requiremnt here matching)
    but when the user say enter during the time of creation of the record the value
    bet01 is not getting defaulted with 10 .........why iam not able to understand
    PLEASE ANY BODY HELP ME IN THIS REGARD 
    regards
    shanti.

    Hi Pranesh,
                   i have used the logic which you hav given as below
    in ZXPADU02
      FIELD-SYMBOLS <fs>  TYPE ANY.
    ASSIGN ('(MP000800)Q0008-betrg') TO <fs>.
    if <fs> is assigned.
    <fs> = l_v_amount .  ---  amount as (ansal /100 i.e 1000/100 = 10) 10
    endif.
    still iam not getting the data for the field bet01
    this is the coding i am using
    data : wa_p0008 like p0008,
            i0008 like p0008,
           l_v_ANSAL type ANSAL_15,
           l_v_amount type PAD_AMT7S.
      FIELD-SYMBOLS <fs>  TYPE ANY.
    CASE innnn-infty.
    when '0008'.
    MOVE-CORRESPONDING  innnn to wa_p0008.
    CALL METHOD cl_hr_pnnnn_type_cast=>prelp_to_pnnnn
    EXPORTING
    prelp = innnn
    IMPORTING
    pnnnn = wa_p0008.
    if wa_p0008-lga01 = 'MFPY'.
    l_v_ansal =  wa_p0008-ansal.
    l_v_amount = l_v_ansal / 100.
    move l_v_amount to wa_p0008-bet01.
    ASSIGN ('(MP000800)Q0008-betrg') TO <fs>.
    if <fs> is assigned.
    <fs> = l_v_amount .
    endif.
    CALL METHOD cl_hr_pnnnn_type_cast=>pnnnn_to_prelp
    EXPORTING
    pnnnn = wa_p0008
    IMPORTING
    prelp = innnn.
    endif.
    when others.
    endcase.
    please help me as iam still not getting (value in bet 01) when i press enter once i enter value in ansal feild.
    regards
    shanti.

  • HT5622 When trying to log on to I-Cloud with my Apple Id, I keep getting the message that this is a valid Apple Id but not an I-cloud account???How do I link my I-Tunes Apple Id with I-Cloud?

    When trying to log on to I-Cloud with my Apple Id, I keep getting the message that this is a valid Apple Id but not an I-cloud account???How do I link my I-Tunes Apple Id with I-Cloud?

    You are logging in iCloud for the first time. In this case, first you need to log in iCloud on an iPhone, iPad, iPod touch or Mac. See > http://www.apple.com/icloud/setup
    If you have not got a device to set iCloud up, you cannot use it. You can only use iCloud on your PC if you have got an Apple device

  • When I go into More 4 Catch up TV I do not get the bar that allows you to pause, enlarge, scoll forward when I click onto a programme is this Firefow or More 4? Can you help me fix?

    When I go into Channel 4 website to catch up on tv programmes on any of their channels I do not get the task bar to be able to pause, select point in programme to start from etc. Happens in both play and play in pop up. So if I play I can not make large screen.
    Don't know where problem lies but others in house who acces same web site do not have this problem and I am the only one that uses firefox.
    We all have different computers

    That file might have moved to a different location on the server. I think you'll need to check with RefWorks support or your local IT department on why the install page links do not work.

  • I Just received the update to iTunes, when I I look at the screen I do not see the normal icons.  When I select movies I do not get the new downloads unless i select the list in the sub file. How do I get backto the old method of seeing and using iTunes??

    I Just received the update to iTunes, when I I look at the screen I do not see the normal icons.  When I select movies I do not get the new downloads unless I select the list in the sub file. How do I get back to the old method of seeing and using iTunes??

    I can tell you that this is some of the absolutely worst customer service I have ever dealt with. I found out from a store employee that when they are really busy with calls, they have third party companies taking overflow calls. One of those companies is Xerox. What can a Xerox call center rep possibly be able to authorize on a Verizon account?  I'm Sure there is a ton of misinformation out there due to this. They don't note the accounts properly or so everyone can see them. I have been transferred before and have asked if they work for Verizon or a third party also and was refused an answer so, apparently they aren't required to disclose that information. I spent a long time in the store on my last visit and it's not just customers that get the runaround. It happens to the store employees as well and it's beyond frustrating.

  • I have tried to view videos created in premiere elements 10 and I get the following message: "this file type is not supported, or the required codec is  not installed.  When it opens there is a red screen in the monitor panel with Korean writing which als

    I have tried to view videos created in premiere elements 10 and I get the following message: "this file type is not supported, or the required codec is  not installed.  When it opens there is a red screen in the monitor panel with Korean writing which also appears in the place of each clip in the video.  I tried uninstalling and reinstalling premiere elements 10, but that did not have any effect on the video.  Do you have any suggestions?  I researched codec, but do not understand them at all.

    gloucester
    In case you did not find it, the following is a copy/paste of the Announcement on Premiere Elements 19/NVIDIA GeForce
    that appears at the top of this forum.
    Premiere Elements 10 NVIDIA Video Card Driver Roll Back
    If you are a Premiere Elements 10 user whose Windows computer uses a NVIDIA GeForce video card and you are experiencing
    Premiere Elements 10 display and/or unexplained program behavior, then your first line of troubleshooting needs to be rolling
    back the video card driver version instead of assuring that it is up to date.
    Since October 2013 to the present, there have been a growing number of reports about display and unexplained workflow
    glitches specific to the Premiere Elements 10 user whose Windows computer has a NVIDIA GeForce video card. If this applies
    to you, then the “user to user” remedy is to roll back the NVIDIA GeForce video card driver as far as is necessary to get rid of
    the problems. The typical driver roll back has gone back as far as March – July 2013 in order to get a working Premiere
    Elements 10. Neither NVIDIA nor Adobe has taken any corrective action in this regard to date, and none is expected moving forward.
    Since October 2013, the following thread has tried to keep up with the Premiere Elements 10 NVIDIA reports
    http://forums.adobe.com/thread/1317675
    Older NVIDIA GeForce drivers can be found
    http://www.nvidia.com/Download/Find.aspx?lang=en-us
    A February 2014 overview of the situation as well as how to use the older NVIDIA GeForce drivers for the driver roll back can be found
    http://atr935.blogspot.com/2014/02/pe10-nvidia-video-card-roll-back.html
    ATR

  • Help! I get the message "sorry this video is not available on this device"

    Help! Whenever I attempt to access videos posted on my subscribed YouTube  sites, on my iPad mini, I get the message "sorry this video is not available on this device", on a black screen.. Strange because they always were available up until yesterday. Old iPad 1 is ok.
    I have reset everything. It's annoying because some old vids still play and videos that I'm alerted to on my email will open if I use my old iPad 1.
    Any suggestions from anyone on how to fix the problem? Is it an adobe thing? I can't access anything adobe.
    It's all very frustrating.

    This works!!!!
    To solve the problem follow the instuction:
    Go to Settings
    Go to iTunes & App Stores
    Click on your Apple ID
    In poped up window click Sign Out.
    Fill your Apple ID and Password and press Sign In
    Done. Now you will be able to download applications from App Store

  • I can't sync my Iphone 6plus to Itunes. Ikeep getting the message that this computer is not autherized and go to store and autherrize computer. I do that and get the message that this computer is already autherized, same result after trying again

    why do I keep having the problem of authorization. I only have 3 devices authorized when I am allowed 5. I can't sync my I Phone 6 plus on I Tunes. When I try to sync I get the message that this computer is not authorized and I have to go to the store tab and authorize the computer. I do that and then get the message that this computer is already authorized. I try to sync again and get the message that this computer is not authorized and go yo the store tab. It's a never ending story. How do I fix this?

    Hello lenmin,
    Thanks for using Apple Support Communities.
    To help resolve this issue where you're repeatedly prompted to authorize with your Apple ID in iTunes when syncing, please follow the directions in the article below.
    iTunes: Missing folder or incorrect permissions may prevent authorization - Apple Support
    Have a great weekend,
    Alex H.

Maybe you are looking for

  • Can't send SMS from iPhone 4, iOS 5.0.1

    Hello! I have an iPhone 4 with iOS 5.0.1 simfree. I can recieve sms but I can't send one. It seems like there are issues with imessage, 'cause it says "waiting for activation" and doesn't offer me use my phone number to send sms via, but only my e-ma

  • All apps crash when accessing internet... help!

    Hi all! Need help! All apps are crashing when they try to access the internet... Oddly, I can access the internet just fine e.g. browse with firefox. For example, if I open itunes while connected to the internet it crashes, but if I turn off internet

  • V570 No wake up, No video and now... no power

    I've got a V570 that is about a month old or maybe a little less. It ran no sweat all yesterday. I let it be at night after checking my emails one last time and I assumed it would just go to sleep as it normally does but I can't really confirm that i

  • BarcodeEncoder class:  java.lang.ClassNotFoundException

    Hi, I am trying to add barcoding capability to our XML Publisher based report. We are trying to use Code128 barcode font and I have gone through the drill detailed out by Monsieur Dixter (thanks to him) on his blog and the XML Publisher documentation

  • Include a message-id in database

    Hi, I have created a JDBC to FILE scenario in sap-xi. i just want to put message id in database field corresponding to each records which is picked up by  the jdbc adapter and send it to receiver adpater. Plz help me out. Thanks & regards, Vanita Jai