If any, what class specific actions happen when declaring an empty object field of a given class in java?

StackOverflow https://stackoverflow.com/questions/19702647

  •  02-07-2022
  •  | 
  •  

Imagine a hierachical set of Classes, where Class A has a field Class B, and Class B has field of Class C. The fields are set in the constructor of each class.

Now if I create an object instance of a Class X, with a field "a" of Class A, where "a" is never set, and remains null:

If any, what class specific "actions" happen from the object "a"? Will it call anything at all from it's own fields? Does Class B or C react at all? I imagine that there might be memory allocation or similar, but I am not sure at all. The reason why I am asking, is to get a better understanding of the data flow, and sequence of actions in applications.

I have tried to find an answer to this question for a while, but I can't seem to find the right way to ask, as the question is a little too close to basic questions about how to define objects in Java.

有帮助吗?

解决方案

So lets assume the following classes definition:

public class A {
    private B b;

    public A() {
        b = new B();
    }
}

public class B {
    private C c;

    public B() {
        c = new C();
    }
}

public class C {
    public C() {
    }
}

public class X {
    private A a;

    public X() {
    }
}

Now let assume that the following main is being executed:

public static final void main (String[] argv) {
    X x = new X();
}

Here an instance of X is create in the memory heap and a reference to this object is store in the x variable.

Since no value is assign to the a variable during the class construction, then no instance of A is created. The a variable still takes up space in memory as part of the instance of X that was created (in other words, it still needs enough space to be able to store a reference), but in this case a is assigned the null value (from Java Language Specification §4.12.5)

For all reference types (§4.3), the default value is null.

Now lets modify the X class as follow

public class X {
    private A a;

    public X() {
        a = new A();
    }
}

If we were to execute the main() method again with this modified version of X, then as part of the construction process, X would cause an instance of A to be created which would cause an instance of B to be created which in turn would cause an instance of class C to be created. All these instances would take up space in the memory heap and the reference to these objects would be stored in their respective variables.

其他提示

Rather than thinking of reference-type variables as holding "pointers", I think it more helpful to think of them as holding object identifiers. If X is a variable of class type Thing, then X holds either information sufficient to identify an instance of Thing or a class derived therefrom, or else information sufficient to say that it does not. Although reference-type variables in many Java implementations hold pointers of some sort, there's no requirement that they do so. A Java implementation which wanted to access more than four gigs of memory without having to use 64-bit object references could round all object sizes up to the next multiple of 16 bytes and then have each non-zero object reference store a scaled offset into the heap (so an object which is located 32016 bytes above the start of the heap would store the number "2001" [decimal] as a reference). Although Java doesn't say what the bit pattern associated with a reference means, the one thing it does specify is that the bit pattern with which array slots and object fields are initialized will never identify any object.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top