The infamous object bug

I discovered what i think is a bug whilst using netbeans. When i call up my method to sort an array containing names(ob.sort) in alphabetical order it automatically sorts another array which contains the original names when it isn't supposed to as the original names is not assigned to anything after it has been populated with input at the beginning(ob.input). I experienced this problem whilst writing larger programs(encountered more than once), but i made a simpler one to demonstrate this problem. It looks like much as i copied the class methods an pasted it below the main class making it easier for you to trace the variables in the program.

public static void main(String args[]){
    ObjectTest ob = new ObjectTest();
    ob.input();
    String x[] = ob.getNames();
    System.out.println(x[0]);
    ob = new ObjectTest(x);
    System.out.println(x[0]);
    ob.sort();
    System.out.println(x[0]);
    String y[] = ob.getNamesrt();
    System.out.println(x[0]);

}
}
    /*import java.io.*;
import javax.swing.*;
public class ObjectTest {
String name[];
String namesrt[];

public ObjectTest(){
    name = new String[3];
    namesrt = new String[3];
}

public ObjectTest(String j[]){
    namesrt = j;
}

public void input(){
    for(int i = 0; i < name.length; i++){
        name[i] = JOptionPane.showInputDialog("Enter name");
    }
}

public void sort(){
    if(!(namesrt == null)){
    for(int i = 0; i < namesrt.length; i++){
        for(int c = i + 1; c < namesrt.length; c++){
            if(namesrt[i].compareToIgnoreCase(namesrt[c]) > 0){
                String n = namesrt[i];
                namesrt[i] = namesrt[c];
                namesrt[c] = n;
            }
        }
    }
    }
    else{JOptionPane.showMessageDialog(null,"Names not received");}
}

public String[] getNames(){
    return name;
}

public String[] getNamesrt(){
    return namesrt;
}

public void setNames(String j[]){
    name = j;
}

public void setNamesrt(String j[]){
      namesrt = j;
}
}*/
Jon Skeet
people
quotationmark

I discovered what i think is a bug whilst using netbeans.

Well, it may be a bug in your code. It's not a bug in Java or in Netbeans. It's just demonstrating the fact that arrays are reference types in Java, and the way that objects work.

Here's a short but complete program demonstrating the same effect:

public class Test {
    public static void main(String[] args) {
        String[] x = { "hello" };
        // Copy the *reference*
        String[] y = x;
        System.out.println(y[0]); // Prints "hello"
        x[0] = "new value";
        System.out.println(y[0]); // Prints "new value"
    }
}

The values of x and y here are references to the same array object... so if the array is changed "through" x, that change is still visible as y[0].

If you want to make your code create independent objects, you'll want to change this:

public ObjectTest(String j[]){
    namesrt = j;
}

to:

public ObjectTest(String j[]){
    namesrt = j.clone();
}

(Ideally change it to declare the parameter as String[] j, or better yet fix all your variable names to be more meaningful, but that's a different matter.)

people

See more on this question at Stackoverflow