the corner office

a blog, by Colin Pretorius

« Font Hell | Main | Eclipse and September »

Synthetic accessors

I haven't run into it too often in the past, because I'm normally not too big on nested classes, but since I've been playing with Eclipse RCP and doing GUI stuff, it comes up all the time. I'm talking about the Eclipse compiler warning that gives you this:

Read access to enclosing field is emulated by a synthetic accessor method. Increasing its visibility will improve your performance

It happens when you're accessing a private instance variable of an outer class, from within a nested class. Oer. Performance affecting... can't have that. I'm firmly in the 'fix all compiler warnings' camp, and if my Problems view isn't empty, warnings or otherwise, then I'm not happy. I wasn't really liking the sorts of workarounds needed to silence the warning though, and I wondered how bad it could be, so I decided to do some homework.

There are a great many references to the problem, but the two most enlightening discussions are one from the ADVANCED-JAVA mailing list archives, and one from the Eclipse jdt-dev newsgroup.

Since the referenced variable is actually private, and since nested classes are actually compiled to be separate classes, there's no way for the nested class to directly access the variable. Eclipse is warning you that the compiler has to work around the visibility problem by creating a synthetic accessor (ie. add a getter method) to the outer class, which the nested class then calls.

... and that's all it is. An extra method call is needed to reference the instance variable, and since method calls are more expensive than directly accessing a field, not having the accessor method will 'improve your performance'. OK technically true, but this isn't 1970 anymore, so a method call is somewhat unlikely to be high on your list of things to worry about when it comes to performance. In other words, pffffff.

There is one other potential issue: this special accessor method, albeit obscurely named, means that the private variable is no longer truly private. Yet again, in most, as in most to the power of a bazillion cases, it's simply not an issue.

In other words, the 'synthetic accessor' warning is now one of the handful of Eclipse compiler warnings that I disable and don't feel guilty about.

{2007.08.18 - 23:06}


tech blog


rssfeed posts

© Colin Pretorius