Escape Analysis - Example (Java)

Example (Java)

class A { final int finalValue; public A( B b ) { super; b.doSomething( this ); // this escapes! finalValue = 23; } int getTheValue { return finalValue; } } class B { void doSomething( A a ) { System.out.println( a.getTheValue ); } }

In this example, the constructor for class A passes the new instance of A to B.doSomething. As a result, the instance of A—and all of its fields—escapes the scope of the constructor.

Read more about this topic:  Escape Analysis