Reputation: 1863
I am instantiating an inner class in my constructor but I'm not using it to start threads or as an event listener. For example:
public class Something {
final InnerSomething innerSomething = new InnerSomething();
public Something(Param arg) {
super(arg);
}
private class InnerSomething {...}
...
}
EDIT: I copied & pasted the code from somewhere else and forgot to delete the "abstract" keyword.
EDIT2: "My" definition of "'this' reference escape" is the one found in the book Java Concurrency in Practice.
With the above code, am I allowing the 'this' reference to "escape"?
Upvotes: 4
Views: 327
Reputation: 328737
Technically you are since InnerSomething
will have a reference to this
before Something
's constructor returns.
However if you don't publish the reference and don't do anything with it in InnerSomething
's constructor, it should not create problems. Note that it would be better to mark innerSomething
private as one could access this
through the package-private field.
If you refer to JCiP #3.2.1, they state:
More specifically, the
this
reference should not escape from the thread until after the constructor returns. Thethis
reference can be stored somewhere by the constructor so long as it is not used by another thread until after construction.
Upvotes: 8
Reputation: 308928
I don't believe so. "Escape" means public access to a private reference that's mutable. I don't see that in the code you've posted.
Do we agree on the meaning of the term?
Upvotes: 1