In the 1029 JavaBeans module in exercise 2 source file MyBeanIndedProperties.java is created and this class has the following two getters that operate with the String[] instance field "lines":

      .
      .
      String[] lines = new String[10];
      .
      .
      public String[] getLines() {
            return this.lines.clone();
      }

      public String getLines( int index ) {
            return this.lines[index];
      }

Why would getLines return a ref to a clone of the array ref'd by lines, while getLines returns a ref to the specific String in the lines array?The clone operation only clones the array object: the array elements are copies of the references, so both the orginal and cloned arrays contain the same String object references.

In other words, why wouldn't the code for getLines() be like this?

     public String[] getLines() {
         return lines;
     }

Or, alternatively, why wouldn't getLines(int) be like this?

     public String[] getLines(int index) {
         return this.lines[index].clone();
     }

Is this most likely a little inconsistency in the code, or is there something about array semantics that I'm missing that makes this difference useful?

-Pete Soper

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to