When "this" is assigned a memory location and at what point can a method be called in java

class A { B b; public A() { b = new B(this); //initialization of class A variables } public void meth1() { } } class B { A a; public B(A a) { this.a = a; } } 

I know that this link should not be transmitted this way, but what happens if it is done

Some other classes call the constructor of class A. When is the "this" reference to the actual allocated memory? whether it will be allocated memory as soon as the constructor is called before the call to super () is called.

Suppose class B is a stream, and since B has a reference to A, B can call methods on A before the constructor even returns if the "this" link is not already selected.

+4
source share
5 answers

The memory for the object is allocated until any constructor is executed. Otherwise, the constructor would not have to write the values ​​of the variables.

Therefore, you can pass a link to the current object (aka this ) to other code fragments inside the constructor.

As you noted, the object was not yet completely constructed at that time, and it is a bad idea to actually do it, but "simply" because the values ​​of the object may be in an inconsistent state. A memory has already been allocated and reserved for this object at a given moment in time.

this is just a reference to the "current object", which you can imagine as just another parameter that any non-static method receives. In fact, this is actually how the JVM treats it. See JVMS §2.6.1 Local variables :

When a method method is called, the local variable 0 is always used to pass a reference to the object to which the instance method is called ( this in the Java programming language).

Thus, the direct response to “when this highlighted” is effective: whenever you call a method on an object.

+3
source

this applies to the current object and any object is allocated memory using the "new"

+1
source

Memory is allocated when the JVM processes the new statement. If, for example, your code looks like this:

 A a = new A(); ^ here the memory for A is allocated 

It may actually be a problem to pass this to B Constructor B can invoke the instance method of A until constructor A completes. To avoid possible problems, move the line to the end of constructor A Alternatively, you can control the life cycle of an object from the outside using seters.

0
source

this is assigned before the constructor is called. Actually calling super() not required. This guarantees only the creation of material of the parent class, which does not matter if the parent class is Object . In addition, A methods can be used as soon as an object is created (even before the constructor is called), therefore, if B received a reference to A in the constructor, it can use A methods, just like A itself in the constructor. Just remember to make methods A so that they can be used when A is not fully initialized, or just create and run B after initialization is complete.

0
source

Until you change A or calling methods to A or its members in constructor B , it will work. (See Other Answers)

If you call a method on an incompletely initialized object (after construction), it does not determine what happens. Especially if you use multiple threads (see Memory Barrier).

More on this topic: How do hidden JVM memory barriers behave when creating designer chains?

0
source

All Articles