4

我在理解非静态变量的处理方式时遇到了问题。我选择使用数组,以便轻松检索其内存地址。

考虑以下代码:

public class tryClass
{
    int[] v = {0}; // vector v is non-static (and NOT local to any method)
    tryClass obj;
    public void met ()
    {
        obj = new tryClass();
        obj.v[0] = 30;
        v[0]=3;
    }
    public static void main (String[] args)
    {
        tryClass obj = new tryClass(); // is this the SAME object as in met() ?
        int[] v = new int[1];
        obj.v[0] = 40;
        obj.met();
    }
}

为了了解每一步如何v处理向量,我在代码中填写了一些println指令,我的输出如下:

In main(), BEFORE running met()
    obj.v[0] = 40
    obj.v    = [I@78456a0c

INSIDE method met()
    obj.v[0] = 30
    v[0]     = 3
    obj.v    = [I@15357784
    v        = [I@78456a0c

In main(), AFTER running met()
    obj.v[0] = 3
    obj.v    = [I@78456a0c

我对很多事情感到很困惑,首先是为什么静态方法中obj.v调用时的引用与非静态方法内部的引用main()相同。此外,在没有对象的情况下调用(当然是在非静态上下文中)究竟是什么?vmet()v

我是Java新手,我真的有很多问题,我希望答案可以完全解决它们......提前感谢您的帮助。

为了完整起见,完整的代码是

public class tryClass
{
    int[] v = {0};
    tryClass obj;
    public void met ()
    {
        obj = new tryClass();
        obj.v[0] = 30;
        v[0]=3;
        System.out.println("\nINSIDE method met()");
        System.out.println("\tobj.v[0] = "+obj.v[0]);
        System.out.println("\tv[0]     = "+v[0]);
        System.out.println("\tobj.v    = "+obj.v);
        System.out.println("\tv        = "+v);
    }
    public static void main (String[] args)
    {
        tryClass obj = new tryClass();
        int[] v = new int[1];
        obj.v[0] = 40;
        System.out.println("In main(), BEFORE running met()");
        System.out.println("\tobj.v[0] = "+obj.v[0]);
        System.out.println("\tobj.v    = "+obj.v);
        obj.met();
        System.out.println("\nIn main(), AFTER running met()");
        System.out.println("\tobj.v[0] = "+obj.v[0]);
        System.out.println("\tobj.v    = "+obj.v);
    }
}
4

3 回答 3

1

为什么在静态方法main()中调用obj.v的引用与在非静态方法met()中的v引用相同?

答:因为您没有将其重新分配给内存中的其他对象。即使您在内部更改了数组的内容,它仍然指向内存中的相同“数组”对象。

查看您的代码,并附上我的评论:

public class tryClass
{
    // here, non-static variable v will be instantiated
    // as an array with a length of one, holding the value 0 in it's one slot;
    // it will be instantiated when an instance of tryClass is created.
    int[] v = {0};

    // here, this tryClass has another tryClass named "obj" in it as one of its fields.
    tryClass obj;

    public void met ()
    {
        // here, the tryClass's tryClass obj is instantiated
        // and this second tryClass's "v" is instantiated
        // and then it's one slot is set to 30.
        obj = new tryClass();
        obj.v[0] = 30;

        // now, the first tryClass's "v" is set to 3.
        v[0]=3;
    }

    public static void main (String[] args)
    {
        // creating a new tryClass.  This is NOT the same object as in met.
        // But it CONTAINS the same object in met.
        // You could call it by going obj.obj.
        tryClass obj = new tryClass(); // is this the SAME object as in met() ?  Answer: No.

        // this does nothing, it just creates another int[] v
        // that exists only inside the main() method.  It is not
        // the same as obj.v!
        int[] v = new int[1];

        // changing the contents of obj.v, but not reassigning obj.v itself.
        obj.v[0] = 40;

        // calling met, which will change obj.v's contents again, but not reassign it.
        obj.met();
    }
}

数组是可变的,这意味着即使它在内存中保持相同的对象,它的内容也可以更改。

于 2013-10-03T14:58:47.137 回答
1

您还应该打印出obj引用的值,当不在静态上下文中时,也应该打印出引用的值this。这将表明,您正在为类的不同实例打印引用v值......tryClass

public class tryClass
{
    int[] v = {0};
    tryClass obj;
    public void met ()
    {
        obj = new tryClass();
        obj.v[0] = 30;
        v[0]=3;
        System.out.println("\nINSIDE method met()");
        System.out.println("\tthis     = "+this); //add this
        System.out.println("\tobj      = "+obj);  //add this
        System.out.println("\tobj.v[0] = "+obj.v[0]);
        System.out.println("\tv[0]     = "+v[0]);
        System.out.println("\tobj.v    = "+obj.v);
        System.out.println("\tv        = "+v);
    }
    public static void main (String[] args)
    {
        tryClass obj = new tryClass();
        int[] v = new int[1];
        obj.v[0] = 40;
        System.out.println("In main(), BEFORE running met()");
        System.out.println("\tobj      = "+obj); //and this
        System.out.println("\tobj.v[0] = "+obj.v[0]);
        System.out.println("\tobj.v    = "+obj.v);
        obj.met();
        System.out.println("\nIn main(), AFTER running met()");
        System.out.println("\tobj      = "+obj); //and also this
        System.out.println("\tobj.v[0] = "+obj.v[0]);
        System.out.println("\tobj.v    = "+obj.v);
    }
}

您也可以为该类创建一个构造函数:

public tryClass() {
    System.out.println("Creating new instance of tryClass!");
}

看看发生了什么...

(或者,作为一种学习体验,您可以查看 IDE 中的调试器。这会派上用场很多......)

于 2013-10-03T15:00:38.300 回答
1

当您执行静态 main 方法时,您要做的第一件事是创建一个 tryClass 实例,然后打印数组中一个名为 v 的元素的值,它是该实例的一个属性。(obj.v[0])。然后调用该实例的方法,因此该方法主体中的非静态引用引用该实例的属性(您在 main 方法上创建的属性)。

于 2013-10-03T15:01:49.890 回答