Passing Objects to methods, why aren't they changed permanently?

If an object is passed by reference, and primitive types are passed by value; then why does the following programme not alter the original values of A and B?
I m trying to pass parameters into a method so that they can be altered and returned, can any one suggest a way to do this.
Thanks,
Ben.
public class Application2 {
Application2(){ }
     void goProc(){
          Integer A=new Integer(4);
          Integer B=new Integer(6);
          add(A,B);
          System.out.println("A2 " + A);
          System.out.println("B2 " + B);
     void add(Integer A,Integer B){
          System.out.println("A " + A);
          System.out.println("B " + B);
          A=A.valueOf(String.valueOf((A.intValue()+B.intValue())));
          System.out.println("added " + A);
//Main method
     public static void main(String[] args) {
          Application2 App = new Application2();
          Integer A=new Integer(4);
          Integer B=new Integer(1);
          App.add(A,B);
          System.out.println("A1 " + A);
          System.out.println("B1 " + B);
          App.goProc();
OUTPUT.
A 4
B 1
added 5
A1 4
B1 1
A 4
B 6
added 10
A2 4
B2 6

If an object is passed by reference,It isn't. Instead, a pointer to the object is passed
by value.That is exactly what passing by reference is.
Objects passed by reference can be modified and it will affect the caller, but modifying the reference (assiging a new object) won't affect the caller.
I m trying to pass parameters into a method so that
they can be altered and returned, can any one
suggest a way to do this.I think this is bad OO programing, it results in less readable code and may be hard to debug. Better is to make the method return the result and let the caller assign the new value. (This is my personal oppinion at least)

Similar Messages

  • HT204406 I used Match, and now there are artists missing when I search by artist, yet they are in my library when I search by song. If they are in many song list,  why aren't they in my artist list? Also I can't sort my songs by artist on iPhone,  so thes

    I used Match, and now there are artists missing when I search by artist, yet they are in my library when I search by song. If they are in many song list,  why aren't they in my artist list? Also I can't sort my songs list by artist on iPhone,  so their music is basically lost in space....help

    It's not clear from your post, but you do realise
    that if you import a cd as a compilation, it will not
    appear under artists? This is done to save the iPod
    being cluttered up with single artists entries who
    might have only one song listed.
    I wasn't aware of that. It's true that I haven't been using the "compilation" tag for a long time. I've only started using it with iTunes 7 as otherwise with Coverflow I would have one album cover for each artist in a compilation. Before that my compilations weren't tagged as such as that is quite probably why each artist from compilations showed up when browsing by artists.
    Message was edited by: Didou

  • Why aren't they connecting?

    My apple tv says it will find my computer when I turn on sharing... but my computer says it is sharing. Why aren't they connecting?

    Visit > Switch 101: Migrate your Windows files or system to your Mac
    For more on switching to Mac's see > Apple - Support - Switch 101

  • Why aren't they any superscript/subscript controls for text?

    Why aren't they any superscript/subscript controls for text?

    Hello,
    Please refer to Corey's reply on forum post : http://forums.adobe.com/message/4406018
    Hope this helps.
    Regards,
    Sachin

  • HT201317 We had to replace my husbands iphone. Everything but our photos have transferred to the new phone.  I made sure that the new device is showing up in find my iphone.  I have turned on photo stream on all devices.  Why aren't they streaming to new

    We had to replace my husbands iphone. Everything but our photos have transferred to the new phone.  I made sure that the new device is showing up in find my iphone.  I have turned on photo stream on all devices.  Why aren't they streaming to new phone?

    Only photos taken in the last 30 days are transferred using Photo Stream.

  • I have a number of playlists configured to play songs I haven't heard in a while. They then drop off the playlist once they've played. When I use my iPhone, however, the songs stay on the iPhone version of the playlist. Why aren't they syncing properly?

    I don't like Apple's algorithm that constantly pushes the same songs at me, so I have a couple of playlists configured for songs I haven't listened to in a month, two months, etc. This has worked fine for years. Since I've gotten my iPhone 4S, however, things are goofy.  The songs are marked as played and fall off of the playlist on iTunes, but they stay on the playlist on the iPhone, which means they come up again and again in the shuffle of that playlist. Even if I sync the iPhone the songs are still on the version of the playlist on the phone and I can't figure out a way to "refresh" that playlist and get rid of them. I know this is my own particular quirk about my music, but it's worked with every other iPod I've had. I don't know why the iPhone is holding on to a different version of the playlist than the one on iTunes. Is there a setting I'm missing somewhere?
    My iTunes is on a MacBook running Snow Leopard.

    Lynn,
    I had a very similar issue (same playlist).  After several frustrating hours of various combinations I was able to sync by changing the options to "manuall sync.." and then "dragging and dropping" the needed playlist from the iTunes Playlist list to the iPhone button (on my itunes setup/view that was from the far left column list of all of my  playlist in iTunes to the far upper right "iPhone" button).  To sum up "checking" the playlist and syncing didnt work, "drag and droping" the playlist did... at least for now... then I unselected the "manually manage" in the options and re-synced and all playlist were synced correctly.  Good Luck.

  • Looking for a phone replacement, want the Nexus 5 or One Plus One. Why aren't they compatible?

    Hi all,
    I am currently in the process of shopping for an acceptable replacement for my current phone. I am looking to buy a phone full price off contract and use it on the network. A few different ones I am looking at and prefer to have are the One Plus One and the Nexus 5. Now I understand when looking online people have tried popping a VZ sim card into both of these phones but have had no luck successfully connecting. Why is this? (I understand Verizon is a CDMA network)
    When looking on the two websites, both of these phones contain the appropriate LTE radios and have support for the bands that Verizon currently uses. In addition the Nexus 5 also appears to have support for the CDMA radio and can utilize the Band Classes: 0, 1, and 10; (I'm not familiar with the ones that Verizon currently uses, some incite could be helpful?). The actual tech specs found on the two websites can be seen below:
    Nexus 5:
    Networks
    2G/3G/4G LTE
    North America:
    GSM: 850/900/1800/1900 MHz
    CDMA: Band Class: 0/1/10
    WCDMA: Bands: 1/2/4/5/6/8/19
    LTE: Bands: 1/2/4/5/17/19/25/26/41
    One Plus One:
    GSM: 850, 900, 1800, 1900MHz
    WCDMA Bands: 1/2/4/5/8
    LTE Bands: 1/3/4/7/17/38/40
    If those are the correct CDMA bands that Verizon uses on their network, then why does the Nexus 5 not work on the network? Is there anything that can be done to make it work? Secondly, I understand that the One Plus One is not equipped with a CDMA radio, but why does it not connect to the LTE network? In addition I understand that Verizon is in the process of rolling out their VoLTE network, if either the Nexus or the One Plus One was able to connect using just LTE then theoretically you would not need the phone to connect using CDMA and everything would work properly, correct?
    Looking forward to the responses. Have been a customer for years now and would like to continue that, but am starting to loose faith.

    These will never work, and even after VoLTE it probably won't. They're both missing Band 13 which is key to Verizon.
    They want you to buy THEIR phones on THEIR network. They don't make money when you buy those phones at cost. Another reason why the terrible Gnex was over $500 when it launched on Verizon. I'm surprised Verizon is getting another Nexus, but then again, it's now $650 so the appeal of Nexus devices is gone. To some, not all. Hope you can find a replacement.

  • So why aren't they actually fixing the problem?

    any one know why they don't fix the problem with itunes 12 version?  i see im not the only one who has problems with the itunes 12  and their ipod5. (people with the iphones)

    No-one here speaks for Apple, so you're not going to get an answer (or, at least, not an authoritative one) to a question like "any one know why they don't fix the problem with itunes 12?".  If you can post more details about "the problem", what version of iTunes and edition of Windows you're using, etc. then someone may be able to suggest a remedy or a workaround.  If you want to alert Apple to a specific issue then you can use their feedback page to do so.  If your iDevice is under warranty (or you are willing to pay) you can also contact Apple support and arrange to talk to someone from Apple.

  • Why aren't they .equals

    Why are the two things never ever equal, even when they look identical printed out?
    What i think is happening is that if I have an array
    A B C D A
    I am comparing A to B then to C then to D then to A
    deciding there is one less model and discarding A
    then comparing B to C then to D then to A etc
    deciding B is unique so copying it to a new array....
    what is wrong???
    public void identicalModels(String model, int i)
    for(int j = i+1; j<numModels; j++)
    if(model.equals(singleModel[j]))
    same ++;
    System.out.println("increment same");
    System.out.println(same);
    if (same==0)
    model= noIdenticalModels[x];
    x++;
    else
    finalNumModels --;
    same=0;

    Why are the two things never ever equal, even when
    they look identical printed out? The "things"?
    Anyway, a string representation of something is different from the thing itself. A photograph of an elephant is not an elephant. This is probably far too generic an answer than you were looking for.
    Your code frightens me. A lot of what you're modifying are fields (apparently -- they're not declared in the blocks given) but apparently are being used just as global variables. I wish I could get people to realize that this causes bugs. I've seen more bugs caused by a failure to limit scope than anything else, I think.
    I sure hope your application isn't multithreaded.
    Please read this before posting code again:
    http://forum.java.sun.com/faq.jsp#format

  • ToDo's -- Why aren't they in iCal on my iMac and my iPod Touch?

    To anyone that will listen at Apple...
    I’ve been a devoted Apple user for years. I’ve owned 2 different palm pilots and did the best I could to sync them with my Mac (Palm Desktop, The Missing Sync) -- though always with only limited success.
    I’ve always longed for the day Apple would come up with the perfect portable device that could flawlessly sync with my desktop. And after a very long wait, I have finally bought the iPod Touch -- thinking it would be everything I’d always hoped in terms of ease of use, synchronization, and all around perfection! Now, I do indeed believe that my new Touch can do anything -- it blows the buttons off of my old Palm device... BUT... why in the world would the desktop version of iCal, which holds all my very precious and plentiful ToDo’s, not perfectly sync with my Touch’s “Calendar” app so that it includes everything right where it deserves to be??? Why isn’t it just “iCal”??? I understand (after much searching) that apparently (and confusingly!?!) I can have my iCal ToDo’s sent to the Mail app on my Touch... if I have an IMAP email account... which I don’t... but why would you guys set it up that way in the first place?!? Everything else you’ve created is so elegant, so intuitive. What good are To Do’s to me in a mailbox? I’d like them right where I have them on my iMac!?! A beautiful little checklist...
    Since last year, I’ve been looking at all sorts of “To Do” apps, pouring through reviews, hoping for something great to be there. And I could choose one... and I could use it, but... why in the world do I have to???
    I guess this is less of a question and more of a request for the creators of the OS X and the iPhone OS, but could you please, PLEASE make these two wonderful calendars work together, in EVERY way? Could you make them sync wirelessly too? Could you update these two platforms together, and make this work for those of us that really deserve to see this major oversight corrected? I’ve read other’s comments on this subject, and it confounds everyone that it affects... in my mind, it’s simply so... un-Apple of you!
    Don’t make it cost anything, and don’t make me wait long either -- this should have been correct from the start. I will consult with you and help you understand the problem better, if that’s what it takes. But please just get this one little HUGE thing right. For me, and everyone that loves everything Apple!
    Thanks!

    totally agree. I ended up buying 3rd party to do list software, which I then have to synch via wifi with my desktop imac. Why couldn't a ToDo list be built into the ipod?

  • HT1689 Do I have to subscribe to all my podcasts on my iPhone or will they download from my computer and if so, why aren't they? I have iMatch too!

    Do I have to subscribe to all my podcasts (previously subscribed on computer) on my iPhone or will they download through iMatch?

    You can re-download them for free.  See http://support.apple.com/kb/HT2519.

  • My iphone has more songs and playlists than my computer, why aren't they the same

    Why won't all songs and playlists from iphone sync with computer?

    Charles,
    I get a message that i have purchased songs on my iPhone that I should transfer to my playlist before I upgrade the software. Do you know how to do this?

  • Chapter Markers, why aren't they importing

    I'm using the latest versions of DVDSP and FCP and I'm sort of at a standstill over an incredibly basic thing.
    In my FCP sequences, I have created Chapter Markers for my movies. I have exported the FCP sequences using File/Export/Quicktime Movie, and in the options area I have selected All Markers.
    When I open the exported movies in Quicktime, I am able to see that my Chapters are there.
    Then, I then run Compressor on my various movies, again the latest version. In Compressor, I select the two buttons under Extras that enable me to Import DVD Studio Pro Meta Data and also Chapter Markers Only.
    Then, in DVD Studio Pro, I see nothing. No Chapter Markers. I would appreciate any insight into what I'm missing here. Thank you in advance.

    In my FCP sequences, I have created Chapter Markers for my movies. I have exported the FCP sequences using File/Export/Quicktime Movie, and in the options area I have selected All Markers.
    This is method is incorrect.
    In FCP's Export QuickTime Movie Export dialog, you should select DVD Studio Pro Markers in order for the metadata to be written correctly - chapter info on the 2nd of 2 text tracks of a QT movie - for DVDSP. (the 1st of the 2 text tracks contains the primary DVDSP metadata that allows both Compressor and DVDSP to parse chapters for you)
    By selecting All Markers you're writing all metadata and chapter info to a single text track in your QT movie in a way that neither DVDSP and Compressor can parse for chapter markers.
    In Compressor, I select the two buttons under Extras that enable me to Import DVD Studio Pro Meta Data and also Chapter Markers Only.
    No, select one or the other.
    Include Chapter Markers only will allow your .m2v to be compatible with other authoring apps.
    Add DVD Studio Pro Meta-data will restrict your .m2v to DVDSP but - provided you export directly from Final Cut Pro (File > Export > Using Compressor) or Export a QuickTime Movie with DVD Studio Pro Markers embedded - Compressor places an I-frame on every cut made in an FCP Timeline. It also places an I-frame at the start and end of every (video) transition applied. This is, of course, in addition to placing I-frames at every Chapter or Compression marker that you've placed.

  • Why aren't patch changes part of the AU spec?

    I'm working with a Novation Remote25Sl and the new Automap 3 software with Logic. So far, it's kind of working. But kind of not... And it's brought up a question that's nagged me for a while now.
    Why is something as simple as "+/- patch" not a fundamental part of the AU instrument spec? I mean, shouldn't it be dang simple to map a midi controller so that when you're playing MiniMonsta, or the impOSCAR or FM8, etc, you can just hit buttons to go back and forth through the patches without having to grab your mouse?
    We're talking 1980's basic Midi functionality here folks... Anybody out there part of the AU developer community who'd care to comment? I swear, it would make software instruments so much more FUN if they could improve the "no mouse" experience.
    Um, please?
    Any workarounds Im missing? Yes, I know Logic's built in synths have key commands that be assigned to next/previous preset. I use'em all the time. But that's no good for 3rd party AU's. And as I say, wouldn't this be something you'd expect as part of the basic parameters for any instrument? Or do you think they deliberately left it out to discourage people from changing patches in the middle of a track, causing unpredictable CPU spikes, etc?
    Hmmmmm

    While I agree with you (the level of patch handling, although we have massive resources now, is in many other ways inferior to what we had when we were all using MIDI sequencers and hardware instruments) - speaking to instrument developers, realtime patch changing is generally regarded as a headache.
    This is why the favoured approach is to have multiple instruments loaded (eg how Mainstage does patches) and switch between instruments, rather than having one instrument switch patches.
    Now, for most of my instruments, I set them up with Logic settings, specifically so I can have keyboard/MIDI commands to switch patches - but there some instruments that can't be handled like this.
    I wish some DAW developer really took this whole patch/instrument management area for plugins and developed a top-modern, elegant and comprehensive solution.
    Ideally, in Logic please!

  • Why aren't my changes to a .pdf not saving?

    I am running Acrobat Pro 9 for Mac (9.5.4).  I received an unlocked, not signed .pdf brocure.  I made some edits (rotating pages, inserting arrow and comments, etc), "save[d] As" giving it a new name, and closed the file.  Later, I re-opened it, and none of the rotational changes I made had been saved, and a few of the aded/typed comments were either reformatted so as to be unreadable, or were otherwise misplaced.  What am I doing wrong?  Thanks

    That worked. (Pretty counter-intuitive, don't you think? Would make more sense to allow you to "Save" after using the "Rotate" buttons to make the change permanent.) Thanks for the tip.  Now, all I have to do is remember it! 

Maybe you are looking for