Using Factory method to prevent this from escaping during construction

Hi,
Here is an example from Java concurrency in practice :

Screen-Shot-2019-03-13-at-6.26.39-PM.png
public class ThisEscape {
    public ThisEscape(EventSource source) {
} }
source.registerListener(
    new EventListener() {
        public void onEvent(Event e) {
            doSomething(e);
} });

Open in new window

As per my understanding
The above code has the following problem :
1) Since we are making an anonymous class it will contain the this object with it.
2) This is done inside a constructor before the constructor completes the execution and so the object may not be in a proper state.
So there could be a case due to very lucky timing that this was extracted out of Evenlistener and its a corrupted this.
Although once the constructor completes i think this should automatically be pointing to the complete object.

Now the book gives another example to avoid this escape of this


public class SafeListener {
    private final EventListener listener;
    private SafeListener() {
        listener = new EventListener() {
            public void onEvent(Event e) {
                doSomething(e);
} };
}
    public static SafeListener newInstance(EventSource source) {
        SafeListener safe = new SafeListener();
        source.registerListener(safe.listener);
        return safe;
} }

Open in new window


I dont quite understand how this is not escaped here.
Because here again we are using anonymous class to create an object in the same way as earlier.
So here also this should have escaped.
Please clarify.

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
In the first case you register the listener before the object is fully created.

In the second case you first create the object and then you register the listener.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
1) Yes but this object escapes in the second case also.
2) And in first case even though this object escapes to the listener when in a non completed state.
Still its the object pointer and when the object will be created it will be pointing to the correct one only.

I am thinking that the point of this example was to avoid escaping of this.
But this is getting escaped in both the cases anyway
girionisCommented:
In the second case it does not escape. The listener is not registered/started until *after* the object is created.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.