Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final method parameters

 
Christy Keane
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a policy where I always make my method parameters final - this prevents me inadvertently changing the value of them within the method.

However, the method signatures in the DBAccess interface provided by sun don't have this modifier:



I implement the DBAccess interface in a class called Data.

If I insert the final modifier in the implementing methods in Data, the compiler does not complain - it is happy that Data is implementing the method defined in the interface:




Am I risking automatic failure by having the final modifier here?

If I leave it out in the Data class, it makes my code inconsistent.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a policy where I always make my method parameters final - this prevents me inadvertently changing the value of them within the method.

Your policy is at odds with the
Sun's Java Coding Conventions. I would suggest that you adopt that standard.
 
Christy Keane
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, thanks for the reply.

Which convention specifically does this violate?
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I don't see which part of the Sun code conventions is violated either.

Besides, using a final method parameter is an implementation detail, just like "synchronized". So you can do it, no problem.

Also, Christy's policy of making all method parameters final is quite common and not bad practice at all.

Using a "final" method parameter in an interface is redundant. Although it has no influence on the Javadoc or the compiled bytecode it should not be used there.

Kai
 
Christy Keane
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep, that's what I think as well - I'm going to leave them in...thanks Kai.
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christy,

Personally I'm not fond of making method parameters final. But I do think that there is nothing wrong in declaring them final in your implementing class. As long as you don't change the interface that was given to you.

Frans.
 
Tom Henricksen
Author
Ranch Hand
Posts: 144
5
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never heard of this. Is this a common practice for many people?
Thanks,
Tom
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic