X-Received: by 10.224.176.196 with SMTP id bf4mr5769538qab.4.1360437018721; Sat, 09 Feb 2013 11:10:18 -0800 (PST) X-Received: by 10.49.95.68 with SMTP id di4mr729725qeb.0.1360437018703; Sat, 09 Feb 2013 11:10:18 -0800 (PST) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!p13no5164573qai.0!news-out.google.com!k2ni21154qap.0!nntp.google.com!p13no5164568qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Sat, 9 Feb 2013 11:10:18 -0800 (PST) In-Reply-To: <5115d723$0$295$14726298@news.sunsite.dk> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T NNTP-Posting-Host: 173.164.137.214 References: <3c0d69c3-591d-4d99-8c13-30a0fd1684b3@googlegroups.com> <510db1c3$0$282$14726298@news.sunsite.dk> <510ea3f6$0$9502$9b4e6d93@newsspool1.arcor-online.net> <5115d723$0$295$14726298@news.sunsite.dk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6eb80ab4-30fb-4137-b7e0-1dcca2ca7fbc@googlegroups.com> Subject: Re: Design Patterns From: Lew Injection-Date: Sat, 09 Feb 2013 19:10:18 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:22254 Arne Vajh=F8j wrote: > A plain nested class does not have access to the local > variables. >=20 > If that is not needed then fine. >=20 > If that is needed then it has to be either a local > or anonymous class. Nitpick: or inner class. > And local classes are very rare in the real world. Sort of. Named local classes are indeed rare. Anonymous local classes=20 are rather common. public List getFiles() { final FileFilter filter =3D new FileFilter()=20 { @Override boolean accept(File f) { return f.isDirectory(); } }; // instance of an anonymous local class implementing FileFilter ... =20 } This is good Java idiom because the 'FileFilter' instance is GCable when=20 done. It hasn't got state so its cost is virtually nil. So the local class= =20 feature gives you good control of a program's memory requirements. In less educated programmers' code I've seen such implementors scoped at=20 instance or even (gasp) static member level. And then people wonder at the= =20 heap requirements. Java has a funky and nuanced type structure. If you take advantage of=20 the nuances you get a lot in return. It helps to use JLS (Java Language Specification) terminology in its=20 exact, narrowest framing. Really. Some call all nested classes "inner"=20 classes. Nope. And anonymous classes can be member or local types. It's=20 all about scope and context. The JLS has a detailed treatment, and somewhere out there floats a=20 brilliant infographic going back to 1.2-ish days, but here's a brief taxonomy. top level * {public, package}, nested {static, inner {member, local}} * {any access} This is off the top of my head, so any corrections welcomed. There's a lovely corner case involving public class Containing { private void doSomething() {...} public class Within extends Containing { // @Override // ? public void doSomething()=20 {=20 Containing.this.doSomething(); // super.doSomething(); // ? ... } } }