Why last array filled with first array without asking?

I try to hold some value in 1st array, then another value in 2nd array, I try to print 1st array and it returns 2nd array values. Any ideas why ?

this is main method to call program

public class testC
{
    public static void main(String[] args)
    {
        groupC trial = new groupC();
        trial.setCol(10, 10);
    }
}

this is class calls another class to get color and fill in array each new coordinate

public class groupC
{
    private color1 col = new color1();
    private int[] cola1 = new int[3];
    private int[] cola2 = new int[3];

    public groupC()
    {

    }

    public void setCol(int xIn, int yIn)
    {
        cola1 = col.getCol(xIn, yIn);
        System.out.println(cola1[0] + " " + cola1[1] + " " + cola1[2]);
        /* try next color depending on 1st */
        cola2 = col.getCol(xIn + 100, yIn + 100);
        System.out.println(cola2[0] + " " + cola2[1] + " " + cola2[2]);

        System.out.println("this is 1st color, but why now the same as 2nd  ?" + cola1[0] + " " + cola1[1] + " " + cola1[2]);
    }
}

this is class, which simply gets coordinate and return array of color values at that point

import java.awt.Color;
import java.awt.Robot;
import java.awt.AWTException;

public class color1
{
    int[] color = new int[3];

    public color1()
    {
    }

    public int[] getCol(int xIn, int yIn)
    {
        // accepts position of color, returns size 3 array of red green blue
        // integers
        try
        {
            Robot r = new Robot();
            Color x = r.getPixelColor(xIn, yIn);
            color[0] = x.getRed();
            color[1] = x.getGreen();
            color[2] = x.getBlue();
        }
        catch (AWTException e)
        {
            e.printStackTrace();
        }
        return color;
    }
}
Jon Skeet
people
quotationmark

You're only actually populating a single array - and overwriting it every time you call getCol. So the values of cola1 and cola2 end up referring to the same array, whereas you want them to refer to different arrays.

You should probably be creating a new array in getCol, declaring color as a local variable instead of a field:

// Note rename of method to be more descriptive
public int[] getColorComponents(int xIn, int yIn)
{
    int[] components = new int[3];
    // accepts position of color, returns size 3 array of red green blue
    // integers
    try
    {
        Robot r = new Robot();
        Color x = r.getPixelColor(xIn, yIn);
        components[0] = x.getRed();
        components[1] = x.getGreen();
        components[2] = x.getBlue();
    }
    catch (AWTException e)
    {
        e.printStackTrace();
    }
    return components;
}

(As an aside, "handling" exceptions by just catching them, printing a stack trace and then continuing as if nothing had gone wrong is almost never the right approach. Additionally, you should work on your naming - none of your class names describe their purpose, and all violate Java naming conventions.)

people

See more on this question at Stackoverflow