Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java设置变量_Java_Variables_Colors - Fatal编程技术网

java设置变量

java设置变量,java,variables,colors,Java,Variables,Colors,我对下面的代码有一些问题。我的问题是变量bgcrforpnls的值与变量c的值不同。但是它应该可以工作,因为这个变量是一个引用 public static Color BGCLRFORPNLS = Color.BLACK; private static void Initialze() { List<Color> colors = new ArrayList<Color>(); colors.add(BGCLRFORPNLS); Color c

我对下面的代码有一些问题。我的问题是变量
bgcrforpnls
的值与变量
c
的值不同。但是它应该可以工作,因为这个变量是一个引用

public static Color BGCLRFORPNLS = Color.BLACK;
private static void Initialze() {
    List<Color> colors = new ArrayList<Color>();

    colors.add(BGCLRFORPNLS);
    Color c = colors.get(0);

    JOptionPane.showMessageDialog(null, "hashcode of c: "+ c.hashCode());
    JOptionPane.showMessageDialog(null, "hashcode of BGCLRFORPNLS: "+ BGCLRFORPNLS.hashCode());

    c = Color.red;

    JOptionPane.showMessageDialog(null, "color of c: "+ c.toString());
    JOptionPane.showMessageDialog(null, "color of BGCLRFORPNLS: "+ BGCLRFORPNLS.toString());
}
statement                   |  BGCLRFORPNLS |     colors[0]     |     c
---------------------------------------------------------------------------------
BGCLRFORPNLS = Color.BLACK; |  Color.BLACK  |     ----          |      ------
colors.add(BGCLRFORPNLS);   |  Color.BLACK  |    Color.BLACK    |      ------
Color c = colors.get(0);    |  Color.BLACK  |    Color.BLACK    |   Color.BLACK
c = Color.red;              |  Color.BLACK  |    Color.BLACK    |   Color.red
公共静态颜色bgcrforpnls=Color.BLACK;
私有静态void initialize(){
列表颜色=新的ArrayList();
添加(BGCLRFORPNLS);
颜色c=颜色。获取(0);
showMessageDialog(null,“c的hashcode:+c.hashcode());
showMessageDialog(null,“BGCLRFORPNLS的hashcode:+BGCLRFORPNLS.hashcode());
c=颜色。红色;
showMessageDialog(null,“c的颜色:+c.toString());
showMessageDialog(null,“BGCLRFORPNLS的颜色:+BGCLRFORPNLS.toString());
}

不,它不应该工作,因为
c
是对
Color
对象的引用,当您更改它时,您不会更改对该对象的任何其他引用,也不会更改该对象的值,只会更改特定的引用

public static Color BGCLRFORPNLS = Color.BLACK;
private static void Initialze() {
    List<Color> colors = new ArrayList<Color>();

    colors.add(BGCLRFORPNLS);
    Color c = colors.get(0);

    JOptionPane.showMessageDialog(null, "hashcode of c: "+ c.hashCode());
    JOptionPane.showMessageDialog(null, "hashcode of BGCLRFORPNLS: "+ BGCLRFORPNLS.hashCode());

    c = Color.red;

    JOptionPane.showMessageDialog(null, "color of c: "+ c.toString());
    JOptionPane.showMessageDialog(null, "color of BGCLRFORPNLS: "+ BGCLRFORPNLS.toString());
}
statement                   |  BGCLRFORPNLS |     colors[0]     |     c
---------------------------------------------------------------------------------
BGCLRFORPNLS = Color.BLACK; |  Color.BLACK  |     ----          |      ------
colors.add(BGCLRFORPNLS);   |  Color.BLACK  |    Color.BLACK    |      ------
Color c = colors.get(0);    |  Color.BLACK  |    Color.BLACK    |   Color.BLACK
c = Color.red;              |  Color.BLACK  |    Color.BLACK    |   Color.red

不,它不应该工作,因为
c
是对
Color
对象的引用,当您更改它时,您不会更改对该对象的任何其他引用,也不会更改对象的值,而只更改特定引用

public static Color BGCLRFORPNLS = Color.BLACK;
private static void Initialze() {
    List<Color> colors = new ArrayList<Color>();

    colors.add(BGCLRFORPNLS);
    Color c = colors.get(0);

    JOptionPane.showMessageDialog(null, "hashcode of c: "+ c.hashCode());
    JOptionPane.showMessageDialog(null, "hashcode of BGCLRFORPNLS: "+ BGCLRFORPNLS.hashCode());

    c = Color.red;

    JOptionPane.showMessageDialog(null, "color of c: "+ c.toString());
    JOptionPane.showMessageDialog(null, "color of BGCLRFORPNLS: "+ BGCLRFORPNLS.toString());
}
statement                   |  BGCLRFORPNLS |     colors[0]     |     c
---------------------------------------------------------------------------------
BGCLRFORPNLS = Color.BLACK; |  Color.BLACK  |     ----          |      ------
colors.add(BGCLRFORPNLS);   |  Color.BLACK  |    Color.BLACK    |      ------
Color c = colors.get(0);    |  Color.BLACK  |    Color.BLACK    |   Color.BLACK
c = Color.red;              |  Color.BLACK  |    Color.BLACK    |   Color.red

您只需更改参考,即,您将参考c指向对象红色。这对您先前引用的对象绝对没有副作用。

您只是在更改引用,即,您将引用c点指向对象红色。这对您之前引用的对象绝对没有副作用。

jip我理解,但是否有其他方法可以工作?有什么建议吗?@user744329,如果您想更改BGCLRFORPNLS的值,您必须执行BGCLRFORPNLS=Color.red之类的操作。BGCLRFORPNLS不是最终值,因此您可以更改该值。但是也许你应该在这里扩展你真正想要做的事情。jip我理解,但是有没有其他方法可以让它工作呢?有什么建议吗?@user744329,如果您想更改BGCLRFORPNLS的值,您必须执行BGCLRFORPNLS=Color.red之类的操作。BGCLRFORPNLS不是最终值,因此您可以更改该值。但也许你应该扩展你在这里真正想要做的事情。