java - Why I cant' use nested classes/Objects? -


i stuff while coding:

getnamedjdbctemplate().update(sql, new mapsqlparametersource() {          {             addvalue("a", obj.geta());             addvalue("b", obj.getb());             addvalue("c", obj.getc());         }     }); 

or this

getjdbcoperations().queryforobject(sql, new object[] { id}, new rowmapper<obj>(){          @override         public obj maprow(resultset rs, int rownum) throws sqlexception {             // todo auto-generated method stub             return null;         }}); 

i use lot off inner classes java methods.

for sure, if use code more once place on different class can reuse it... problem team mates don't this, , nobody able give me proper reason of why not...

they talk lot memory leaks , garbage collection(gc) believe stuff past. i'm using java6 , spring 3. apps deployed on tomcat 6 server.

there couple disadvantages can think using double brace initialization , anonymous classes in general:

  1. a little bit more memory consumption - every time write new a() { } create new anonymous subclass of has loaded class loader , hence consumes memory. although might important applications memory consumption critical, vast majority program (especially server side programs) java class memory overhead tiny wouldn't take in consideration.

  2. creating anonymous subclass can cause unexpected troubles many frameworks , libraries relying on reflection. might trip equals() method if doesn't expect existence of subclasses. example think how instance new a() { int x = y; } serialized , deserialized? new a() {{ = 7; }} equal new a() {{ = 7; }} (assuming classes a$5 , a$9 respectively). , fourth.

but besides don't see other disadvantages of doing that. both mentioned issues appear in circumstances , can fixed or worked around when needed.

in general prefer anonymous classes one-time usage , double brace initialization lot because makes code nicer, structured , easier read.

p.s. of colleagues yelling @ me :d


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -