Thursday, April 7, 2011

Final is my favorite Java keyword

Final is my favorite keyword and modifier in Java.  For fields, love it even more than private.  Think about it, you don't even need private on your field if it's final.  Not with a truly non-mutable data type at least, so collections and things of that nature excluded of course.

In fact I wish final was the default modifier for all my fields, variables, and parameters.  You should have to announce your intention to overwrite an already initialized object reference.  After years, I find it is a rarity.

There is nothing worse that digging through a large 500 line function and not being able to see what is changed and where.  Even more frustrating when you go to extract a method from said too-large codeblock and cannot do so easily because of the sprawled out variable declarations and initializations and the occasional "I like this variable name so I'll reuse the reference" laziness.

The trouble is it looks so terrible when used properly, which should be almost everywhere!  It looks pretty satisfying on fields, but over all parameters and variables it gets to be a little much.  It can actually make it harder to find the non-final object references.  And if you find one, was it intentional or just overlooked? Change it and find out I guess.

That is why one of my biggest wishes for the Java language is that final be the default and there to be a mutable or similar keyword for the handful of times you need it.  It would be an excellent aid to readers of your code, "watch out this thing is going to change." Compilers could even check to see if you have flagged something mutable and aren't actually mutating it.  That should be a compile error for variables and parameters, perhaps not for fields though.

Obviously we can't exactly do that ... not so directly.

We have some advantages now we didn't have when Java was created.  Annotations.  When Java was created there had to be long and deliberate thinking as to what the defaults should be.  Everyone would have to live with them and they'd last forever.  These days, however, a simple annotation or two on a Java source file could serve as a clean way to change such defaults in an obvious and documented way that can make the code in question far easier to read.  It would be syntactic sugar of course and that annotation and the defaults it specifies would compile away just as imports do.

A small bit of sugar with big payoff in ease and readability.

57 comments:

default said...

Cool, and if all your variables are final, you don't even need to run the code! You can optimize it away completely in the compiler and reduce it to its (constant) result.

tug said...

I agree completely.

Adam Rabung said...

+1000
Two little nits - a private final field is still better than a final field, because it encapsulates/hides the field - minimizes api surface area. Also, "mutable" keyword would not be good because final Objects _are_ mutable, they're just not reassignable.

David Blevins said...

@Adam Good point. Perhaps 'assignable' is the better keyword. In terms of a get method for a final reference to a non-mutable object, was more making a statement of technical necessity rather than one of best practices. Likely high on the list of things we do that hotspot will remove because it isn't technically necessary.

Viktor Klang said...

Ortwin: Well, I kinda need branching, so your fold-into-constant-result will have to wait.

Brian said...

This is exactly the sort of thing you can do for yourself with JSR-308 and the Checkers framework. Write your own @FinalByDefault class annotation and @Mutable field annotation, write a checker to verify your rules, and you're done.

You can start on this today using the JSR-308 prototype now!

timothymattie said...

more or less into collaboration. Wow! Right now probably weigh over head but what do you guess(ebaan) is direction of a WLAn Coova Hotspot is it worth looking up related. API's, FBML's,FBTS'S,and FQL's.Coova has good Open source, and Java credentials! Appreciated!!
timothymattie@gmail.com
twitter.com/timothymattie

Unknown said...

20170619 junda
oakley sunglasses sale
cheap jerseys wholesale
burberry outlet sale
cheap ray ban sunglasses
adidas uk store
michael kors outlet clearance
cheap nba jerseys
true religion outlet
ray ban sunglasses
nike air max 90

eric yao said...

Coach Outlet ED Hardy Outlet Coach Outlet Store Online Kate Spade Outlet Cheap Jordans Coach Purses Coach Outlet Kate Spade Outlet Toms Outlet Louis Vuitton

Habib Malik said...

North East Post Office Result 2017
Chhattisgarh Post Office GDS Result 2017
Uttarakhand Post Office GDS Result 2017
Haryana Post Office GDS Result 2017
Tamil Nadu Post Office GDS Result 2017

Unknown said...

good post

-----------
شركة تنظيف بالرياض

Danish Ahmed said...

Thanks for sharing this article, now you can also check your Recuritment & Notification 2017-2018 by following below given links...

fci andhra pradesh watchman recuritment 2017

fci tamilnadu watchman recuritment 2017

xat notification 2018

jkbote polytechnic result 2017

friv 10 said...


gracias por la detallada información. Friv 2019 Gry Friv 2018 Juegos Friv 3 Gracias por compartir sus ideas con nosotros. Gry Friv 3 Jeux De Friv Gracias por vuestras reacciones a las transmisiones que os han hecho. Friv 2019 Juegos Friv 2019 Acogemos con agrado los comentarios de los lectores

alicetaylor said...

Thanks for the best blog.it was very useful for me.keep sharing such ideas in the future as well. Thanks for giving me the useful information. I think I need it!

http://lennyfacetext.com

Unknown said...

Get the best cleaning service from our company to clean the months and the best cleaning companies in the Kingdom of Saudi Arabia, the company cleaning houses, apartments, villas, companies and factories in all the contents such as floors, walls, windows, kitchens, bathrooms and facades of houses and furniture and bedding and insulation of roofs and reservoirs, you can contact us and request one of our services Get the best cleaning service now
شركة تنظيف بتبوك

شركة تنظيف بضباء

شركة تنظيف ببريدة

شركة تنظيف بأبها

شركة تنظيف بخميس

شركة تنظيف بجده

Unknown said...

A leading company in the field of cleaning as it is doing all kinds of cleaning such as villas, apartments, houses and companies, and we also clean carpet and carpet all with the best equipment and the best workers, we work under a business plan that makes us the best in this area, do not hesitate to contact us and be one of our customers Valued We guarantee your working speed and high quality.
شركة تنظيف بتبوك

شركة تنظيف بضباء

شركة تنظيف ببريدة

شركة تنظيف بأبها

شركة تنظيف بخميس

شركة تنظيف بجده

Unknown said...

A leading company in the field of cleaning as it is doing all kinds of cleaning such as villas, apartments, houses and companies, and we also clean carpet and carpet all with the best equipment and the best workers, we work under a business plan that makes us the best in this area, do not hesitate to contact us and be one of our customers Valued We guarantee your working speed and high quality.
شركة تنظيف بمكة

شركة تنظيف بالقطيف

شركة تنظيف بجازان

شركة تنظيف بنجران

شركة تنظيف بعنيزة

شركة تنظيف ببيشه

Unknown said...

Get the best cleaning service from our company to clean the months and the best cleaning companies in the Kingdom of Saudi Arabia, the company cleaning houses, apartments, villas, companies and factories in all the contents such as floors, walls, windows, kitchens, bathrooms and facades of houses and furniture and bedding and insulation of roofs and reservoirs, you can contact us and request one of our services Get the best cleaning service now
شركة تنظيف بمكة

شركة تنظيف بالقطيف

شركة تنظيف بجازان

شركة تنظيف بنجران

شركة تنظيف بعنيزة

شركة تنظيف ببيشه

Matway said...

شركة تنظيف ببريدة

Amany Said said...

هل تبحث عن شركة متخصصة فى خدمات التنظيف بالطائف بافضل المعدات والسوائل وثقة تمة فى العمل ودقة فى النتائج كل هذه المميزت توفرها شركة الخليج الشركة الافضل والامثل فى الخدمات المنزلية بالطائف وبما اننا الشركة الافضل والامثل بدون منافس سوف نسعى لتوفر افضل الخدمات باقل تكلفة وبقدر كبير من الاهتمام والدقة عزيزى اينما كنت فى اى منطقة ا وحى تابع لمدينة الطائف اتصل بنا وسوف نصلك فى الحال شركة الخليج للخدمات المنزلية شركة تنظيف منازل بالطائف
شركة تنظيف فلل بالطائف
شركة تنظيف خزانات بالطائف
شركة تسليك مجارى بالطائف
شركة رش مبيدات بالطائف
شركة مكافحة نمل ابيض بالطائف
شركة مكافحة حشرات بالطائف
شركة عزل اسطح بالطائف
شركة عزل خزانات بالطائف

Unknown said...

Bangalore escorts

Bangalore escort

Female escort in Bangalore

Independent escort in Bangalore

Russian escort in bangalore

call girls in Bangalore

Bangalore escorts agency

VIP escort agency in Bangalore

Top sexy girl in Bangalore

House escort service in Bangalore

Bangalore escort service

High profile escort in Bangalore

Model escort service in Bangalore

Sexy collage girl in Bangalore

24/7 Available call girl in Bangalore

Cheap call girl in Bangalore

Low and high price escort in Bangalore

Erotic massage escort in Bangalore

Top classic call girl service in Bangalore

sofeeya call girl in Bangalore














Bilibili said...

Pandora Jewelry
Jordan 11
Adidas Yeezy
Pandora Jewelry
Kyrie 3 Shoes
Retro Jordan 11
Air Jordan Retro 9
Nike Air Max 270
Red Bottom Shoes For Women
Rodney20181226

Bilibili said...

Air Jordan 11
Jordan 4
Pandora
Red Bottom Shoes For Women
Yeezy boost 350 v2
Jordan Retro
Pandora Jewelry Outlet
Kyrie Shoes
Nike Air Max 270
Air Jordan 9
Rodney20181231

Bilibili said...

Jordan 9
Air Jordan
Nike Air Max 270
Kyrie Shoes
Air Jordan 9
Pandora Charms Outlet
Jordan 4 Retro
Pandora Jewelry
Red Bottom Shoes For Women
Yeezy boost 350 v2
Rodney20190217

Bilibili said...

Kyrie Irving Shoes
Air Jordan 11
Air Jordan 9
Jordans 11
Yeezy boost 350 v2
Jordan 4 Retro
Jordan 11
Pandora Jewelry Outlet
Air Max 270
Rodney20190313

Hualala said...

Yeezy boost 350 v2
Jordan 11
Jordan Retro 11
Air Max 270
Air Jordan
Red Bottom Shoes
Air Jordan 4 Retro
Pandora Charms
Pandora Outlet
Paul20190318

Coqicoqi said...

Red Bottom Shoes For Women
Pandora Jewelry
Pandora
Pandora Outlet
Jordan Retro 11
Air Max 270
Jordan 11
Kyrie Irving Shoes
Adidas Yeezy
Air Jordan Retro 11
Ryan20190325

PalaPala said...

Kyrie Irving Shoes
Jordan 11
Jordan Retro 11
Air Jordan Retro 9
Pandora Jewelry
Air Jordan Retro 11
Red Bottom Shoes For Women
Yeezy boost
Jordan 4
Latrice20190330

oveiya krishnasamy said...

Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.
cloud computing online training

Kiruthiprabha said...

This is very good content you share on this blog. it's very informative and provide me future related information.
Oracle DBA Online Training

rithiaanandh said...

Thanks for sharing the informative concept. Your data visualization contents are really good. You will explain the concept is a very clear and detailed explanation.
Sql server dba online training

jothikumar said...

This is such a good post. One of the best posts that I've read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
Software testing online training
Software testing certification training
Software testing online course
Software testing training course

Online Training said...

Great post and creative ideas. I am happy to visit and read useful articles here. I hope you continue to do the sharing through the post to the reader.

AWS Training

AWS Online Training

Unknown said...

Call Girls in Gurgaon
Call Girl in Gurgaon
Call Girls Gurgaon
Call Girl Gurgaon
Call Girl Gurgaon Service
Call Girls Gurgaon Service
Call Girl in Gurgaon Service
Call Girls in Gurgaon Service
Best Call Girl Gurgaon
Best Call Girls Gurgaon
VIP Call Girls Gurgaon
VIP Call Girl Gurgaon
Gurgaon Call Girl
Gurgaon Call Girls
Russian Call Girls Gurgaon
Russian Call Girl Gurgaon
Call Girls in Dwarka
Call Girl in Dwarka
Call Girls Dwarka
Call Girl Dwarka
Call Girls in Haridwar
Call Girl in Haridwar
Call Girls Haridwar
Call Girl Haridwar
Call Girls in Noida
Call Girl in Noida
Call Girls Noida
Call Girl Noida
plumbing company Toronto
plumbing company Mississauga
https://aqualuxdp.ca/
https://www.torontoplumbinggroup.com/
https://www.singledate.in/
https://muskangirlsdwarka.in/
https://rishikeshgirls.in/call-girls-in-haridwar/
https://shipranoida.in/
Call Girls in Gurgaon | Call Girls Gurgaon | Call Girl Gurgaon | Call Girl in Gurgaon

Darren Demers said...

The trouble is it looks so terrible when used properly, which should be almost everywhere! It looks pretty satisfying on fields, but over all parameters and variables it gets to be a little much. It can actually make it harder to find the non-final object references. And if you find one, was it intentional or just overlooked? Change it and find out I guess. cheap t shirts wholesale , wholesale bulk t shirts free shipping , jersey style shirts wholesale , local wholesale t shirts , best bulk t shirts , bulk buy raglan t shirts , wholesale childrens clothing in bulk , designer baby clothes online , cheap dresses for kids , where can i buy baby clothes wholesale

Anjali Arya said...




The agency follows a strict selection procedure and each and every female interested in joining us need to meet the essential conditions and fulfill the eligibility criteria.Escorts in AgraEscorts in Agrawe have and There is a long list of elite customers who has blind faith in us. It is because of the uncompromising terms and conditions that we strictly follow to ensure highest level of privacy. Check our other Services...
Female Escorts in Agra
Female Escorts in Agra
Russian Escorts in Agra
Escorts in Agra
Female Escorts in Agra

Escorts In Lahore said...



Elite Class Fashionable Well Educated Hot Sex 👙 Models Service Provider Madam Dolly lahore call girls
I Love All Countries Provider Escorts Service best Vip Call Girls in lahore
Full hot and sex excellent girls in available Hotel door service Pakistani lahore Call Girls in lahore
Pakistani lahore vip call girls best female professional call girl models in available vip Hotel best service in Pakistani
call girls in Lahore we have many Independent call girls in Lahore. Our call girls are not road side call girls.
Safe Service call girls in Pakistani lahore
We give you best and safe escorts service in Lahore islamabad call girls
Escorts Services 👉 69 sex 👄 Call/ Whatsapp ☛ ☎ ▻ +92 305 3777077 💃 girls Provider Madam Dolly
we serve all over in Lahore, If you want to more safe side I suggest you book an any 4 & 5 star hotel in Lahroe.
Vip Call Girls in Rawalpindi
Call Girls in Rawalpindi
Rawalpindi call girls
Vip Call Girls in Islamabad
Call Girls in Islamabad
Islamabad call girls
Vip Call Girls in karachi
Call Girls in karachi
karachi call girls
escorts service in lahore
escort in lahore
lahore escorts service
escorts service in Pakistani
Pakistani escorts service
Pakistani call girls



lavanya said...

Thank for this blog. I got some knowledge in your blog it’s very useful for me now I started learning more testing on some other sites like. Software Testing Training in Chennai | Software Testing Training in Anna Nagar | Software Testing Training in OMR | Software Testing Training in Porur | Software Testing Training in Tambaram | Software Testing Training in Velachery

bandarq228 said...

manfaatkan gadget anda dan hasilkan uang kapanpun dan dimananpun dengan mudah hanya dengan bermain dominoqq atau bandarqq di situs terbaik asikbandarqq
daftarkan diri anda sekarang juga hanya di http://180.215.13.115

HOT AND SEXY INDEPENDENT ESCORTS IN LUCKNOW said...

THANK YOU FOR VISITING MY WEBSITE:-
russian escorts in gurgaon
housewife escorts in gurgaon
gurgaon escort services
gurgaon escorts
escorts in gurgaon
escort services in gurgaon
gurgaon call girls
call girls in gurgaon

Ms Nikita Agarwal said...

I really loved your post so much that I cant resist writing comment...Please check mine as well
Escorts in Jaipur
Escorts in Jaipur
Escorts in Jaipur
Escorts in Delhi
Escorts in Guwahati
Escorts in Guwahati
Escorts in Guwahati
Escorts in Guwahati
Escorts in Lucknow

Anonymous said...


Kanpur Escorts
Mumbai Escorts
Navi Mumbai Escorts
Noida Escorts
Pune Escorts
Ramagundam Escorts

Thane Escorts
Vasai-Virar Escorts
Ahmedabad Escorts
Bangalore Escorts

Anonymous said...

Shillong Escorts
Shimla Escorts

Siliguri Escorts
Silvassa Escorts
Solapur Escorts
South Dumdum Escorts
Sri Ganganagar Escorts
Srinagar Escorts

Surat Escorts
Trivandrum Escorts

Anonymous said...


Pimpri-Chinchwad Escorts
Port Blair Escorts
Raipur Escorts
Rajahmundry Escorts
Rajkot Escorts
Ranchi Escorts

Rourkela Escorts
Saharanpur Escorts
Salem Escorts
Sangli Escorts

Anonymous said...

Mangalore Escorts
Meerut Escorts
Mysore Escorts
Nagpur Escorts

Nanded Escorts
Nashik Escorts
Nellore Escorts
Kolkata Escorts
Panaji Escorts
Patiala Escorts

Anonymous said...

Kochi Escorts
Kolhapur Escorts

Kozhikode Escorts
Kurnool Escorts
Loni Escorts
Lucknow Escorts
Ludhiana Escorts
Madurai Escorts

Maheshtala Escorts
Malegaon Escorts

Anonymous said...

Hyderabad Escorts
Indore Escorts
Itanagar Escorts
Jabalpur Escorts
Jaipur Escorts
Jalandhar Escorts

Jhansi Escorts
Jodhpur Escorts
Kakinada Escorts
Kalyan-Dombivali Escorts

Anonymous said...

Erode Escorts
Faridabad Escorts
Gangtok Escorts
Gorakhpur Escorts

Gulbarga Escorts
Guntur Escorts
Guwahati Escorts
Gwalior Escorts
Howrah Escorts
Hubli Escorts

Anonymous said...

Bhiwandi Escorts
Bhopal Escorts

Bhubaneswar Escorts
Bikaner Escorts
Bokaro Escorts
Chandigarh Escorts
Cuttack Escorts
Daman Escorts

Dehradun Escorts
Durgapur Escorts

Anonymous said...

Agra Escorts
Akola Escorts
Aligarh Escorts
Allahabad Escorts
Amravati Escorts
Amritsar Escorts

Bareilly Escorts
Belgaum Escorts
Bhavnagar Escorts
Bhilai Escorts

ncrhotgirls.com said...

I would like to be your pen friend...You can write me email on genuinehotelescorts@gmail.com for
Jaipur Escort
Jaipur Escort
Jaipur Escort
Delhi Escort
Guwahati Escort
Guwahati Escort
Guwahati Escort
Guwahati Escort
Lucknow Escort
Aerocity Escort

Unknown said...

Explore the profiles of guwahati escorts, guwahati call girls, call girls in guwahati and
escorts in guwahati for female companionship in top rated hotels.
Get affordable escort services in guwahati hotels now.
Guwahati call girls
Call girls in Guwahati

diyaroy.com said...

Maybe you are also looking for the right person to ensure the higher quality
sex and that’s why you are in this place because here we are going to share
the data and information of Guwahati Escorts.
Guwahati escorts
Guwahati Call Girls

Escorts Service Kolkata said...

escorts service in kolkata |
escorts service in kolkata |
escorts service in kolkata |
escorts service in kolkata |
escorts service in kolkata |

neausmey said...

pop over to these guys replica ysl bags discover this replica designer bags see page gucci replica handbags

Unknown said...

cet article répliques de sacs de créateurs bon marché supplémentaire lecture jetez un œil sur ce site Web consultez le message ici dolabuy.su

Unknown said...

Look At This f8z82d5t22 cheap designer bags replica replica bags paypal accepted replica bags canada gucci replica s2r88y1j71 replica bags lv weblink y9n26g1w05 replica bags replica bags blog y8k49u7z90