spring - java field qualifier best practice -


it practice use proper qualifier between private, protected, private or default. there other reason performance or jvm optimization drawback default used instead of private? example

public class class1{     class2 class2; } 

and variable class2 have been private.

also if variable autowired or injected di framework. framework calls field.setaccessible(true). make difference per performance or optimization.

i think understand motivation question.

the reasons using correct access modifiers on variables in normal java understood. basically, modularity, encapsulation, avoiding unwanted / harmful coupling , on.

what spring?

well true spring can circumvent normal access rules , allow inject private variables. however, understand, have deliberately annotate private fields @autowire or similar occur. going on here spring following "instruction" explicitly declared in source code means of annotation. spring xml-based wiring won't let inject value private field or using private setter.

in light of this, argument spring allows break private encapsulation ... while technically true ... self-serving. sure, can it. have explicitly, deliberately ... design. , happens when objects wired.

by contrast, if sloppy modifiers, , declare every instance variable public or package private, leaving open possibility of sorts of lazy, ill-considered, or accidental breaking of encapsulation. , if (the original author) disciplined, next guy reading / maintaining code can't sure have been disciplined. has check ...

so how "force" toe line?

it best persuade rather force, way force people write decent code project manager / quality manager adopt coding standard, , insist followed. (but can easier said done if management doesn't understand long-term costs of poor quality.)

the real reason have these pesky coding standards code can maintained ... other guy wrote it. manager understand this. pm understand this. programmer understand this.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -