java如何处理非静态变量?

时间:2013-10-03 14:48:20

标签: java oop non-static

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

请考虑以下代码:

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()时的引用与非静态内部v的引用相同方法met()。此外,在没有对象的情况下调用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);
    }
}

3 个答案:

答案 0 :(得分:1)

为什么obj.v的引用在静态方法main()中调用时与非静态方法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();
    }
}

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

答案 1 :(得分: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中查看调试器。这将会派上用场......)

答案 2 :(得分:1)

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