Results 1 to 2 of 2
  1. #1
    Shayke_ is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Elementary question on variable references

    I'm new to Java and just want to make sure I understand when references to objects remain valid. For example, the following code illustrates the question I have in mind.

    Java Code:
    class MyTest {
    
         public static void main(String[] args) {
    
              MyClass foo = new MyClass();
              initialize(foo);
              System.out.println(foo.myString);
         }
    
         static void initialize(MyClass foo) { 
              foo.setString("Hello World"); 
         }
    }
    
    class MyClass {
    
         String myString;
    
         void setString(String str) { myString = str; }
    
    }

    The program compiles and gives the expected output "Hello World." The question is, does Java guarantee the reference to myString will remain valid? The potential problem is that the original string "Hello World" goes out of scope when initialize() is exited.

    My understanding is that Java is smart about this, and that it won't free or overwrite the memory for "Hello Word" until there are no more in-scope references to it anywhere. But I just want to make sure before I mess up too much code.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Shayke_ View Post
    I'm new to Java and just want to make sure I understand when references to objects remain valid. For example, the following code illustrates the question I have in mind.

    Java Code:
    class MyTest {
    
         public static void main(String[] args) {
    
              MyClass foo = new MyClass();
              initialize(foo);
              System.out.println(foo.myString);
         }
    
         static void initialize(MyClass foo) { 
              foo.setString("Hello World"); 
         }
    }
    
    class MyClass {
    
         String myString;
    
         void setString(String str) { myString = str; }
    
    }

    The program compiles and gives the expected output "Hello World." The question is, does Java guarantee the reference to myString will remain valid? The potential problem is that the original string "Hello World" goes out of scope when initialize() is exited.

    My understanding is that Java is smart about this, and that it won't free or overwrite the memory for "Hello Word" until there are no more in-scope references to it anywhere. But I just want to make sure before I mess up too much code.
    Yup, Java is 'clever' enough not to lose a reference to your literal String, i.e. when you call your initalize( ... ) method the stack has a reference to your String; in your method the object sets its myStr variable to your String; note that all the time a reference is available to your String so the garbage collector won't touch it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Handling Exception - elementary question
    By kracer in forum New To Java
    Replies: 2
    Last Post: 05-06-2010, 11:47 PM
  2. Replies: 20
    Last Post: 06-03-2009, 05:33 PM
  3. Elementary problems
    By mgm2010 in forum New To Java
    Replies: 6
    Last Post: 02-01-2009, 08:33 PM
  4. Elementary Quesions
    By hitmen in forum New To Java
    Replies: 6
    Last Post: 10-21-2008, 12:23 AM
  5. Replies: 1
    Last Post: 10-02-2008, 03:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •