passing String references


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: passing String references

  1. #1
    Harry Guest

    passing String references


    Hi, I was wondereing if someone could help me with a problem I am having with
    strings.

    I have a call to OFileName(iFileNm)

    In get OFileNm I have
    private void getOFileNm(String file)
    {
    file = "C:\\jdocs\\j2sdk1_3_0-doc.zip";
    }

    However, String iFileNm does not get changed by the function call. I would
    think that the call would pass the reference to the string, and in the methode
    that reference would get changed to point to "C:\\jdocs\\...", however this
    does not seem to be happening. What am I doing wrong?

    Thanks for any help you can give.
    -Harry

  2. #2
    Patrick Guest

    Re: passing String references


    Hey Harry,

    how about having a cup of coffee and sit back and look over your code again?
    Seemed to work last time hehehe

    Patrick

    "Harry" <harrytuttle89@hotmail.com> wrote:
    >
    >Hi, I was wondereing if someone could help me with a problem I am having

    with
    >strings.
    >
    >I have a call to OFileName(iFileNm)
    >
    >In get OFileNm I have
    >private void getOFileNm(String file)
    > {
    > file = "C:\\jdocs\\j2sdk1_3_0-doc.zip";
    > }
    >
    >However, String iFileNm does not get changed by the function call. I would
    >think that the call would pass the reference to the string, and in the methode
    >that reference would get changed to point to "C:\\jdocs\\...", however this
    >does not seem to be happening. What am I doing wrong?
    >
    > Thanks for any help you can give.
    > -Harry



  3. #3
    Paul Clapham Guest

    Re: passing String references

    Here's what happened. First you had a variable iFileNm that referred to a
    certain string. Then you called the OFileName method, and that caused one
    of its local variables, file, to refer to that same string. Then you
    changed file to refer to a different string. Of course this had no effect
    on the first variable. (file referred to the same string as iFileNm did.
    It didn't refer to iFileNm.)

    Remember that objects in Java are always references to objects. And
    remember that Strings are objects, too. Keep reminding yourself of this
    because it seems to be an unobvious concept.

    You didn't post the entire code of your method but if you wanted it to
    return a String, the way to do that is:

    private String getOFileNm() {
    ... return "blabla";
    }

    Harry <harrytuttle89@hotmail.com> wrote in message
    news:39565ded$1@news.devx.com...
    >
    > Hi, I was wondereing if someone could help me with a problem I am having

    with
    > strings.
    >
    > I have a call to OFileName(iFileNm)
    >
    > In get OFileNm I have
    > private void getOFileNm(String file)
    > {
    > file = "C:\\jdocs\\j2sdk1_3_0-doc.zip";
    > }
    >
    > However, String iFileNm does not get changed by the function call. I

    would
    > think that the call would pass the reference to the string, and in the

    methode
    > that reference would get changed to point to "C:\\jdocs\\...", however

    this
    > does not seem to be happening. What am I doing wrong?
    >
    > Thanks for any help you can give.
    > -Harry




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center