• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does this work? sychronized([Classname].this) {...  RSS feed

 
Spencer J Lee
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm looking over some code and I'm not sure what the following bit does:

What object or instantiation of ClassA does ClassA.this refer to?
Thank you ahead of time for your help.
Spencer
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance of a non-static nested class -- a so-called inner class like ClassB -- always has an enclosing instance of the outer class. An instance of ClassB can't be created without associating it, permanently, with an instance of ClassA (try it.) It is this instance that "ClassA.this" refers to.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually a semantic issue, so I'm moving it to JIG Intermediate.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spencer - this has nothing to do with your original question, so just ignore it unless you're interested. But for EFH:
[EFH]: An instance of a non-static nested class -- a so-called inner class like ClassB -- always has an enclosing instance of the outer class.
Not quite. See JLS 15.9.2 It's possible to define an inner class in a static context, such as inside a static method, static variable initializer, or class initializer. In such cases there's no accessible outer instance. And while it may seem as though a nested class created in a static context should be the same as an implicitly static class, and therefore not an inner class - this is not the case. The rules for "implicitly static" (e.g. something declared as a member of an interface) are not the name as the rules for "declared in a static context". One differene is that an implicilty static class could declare a static member, but an inner class declared in a static context may not:

[ March 04, 2004: Message edited by: Jim Yingst ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:

Not quite.

All right, I give up. I'm off to read the JLS on inner classes.
The funny thing is that when I typed my response above, I included something along the lines of "Of course, now Jim is going to explain to us where I got this wrong." But then I erased it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!