• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final method parameter

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does a final modifier in a method parameter work?
Take a look at the following eg. (Dan Chisholm: Chapter 4 / Exam 1 / Question 6):

ANS: compile-time error at 2.
I have a reasonable understanding of how casting works, when to use a cast etc.
My question is: What difference does the 'final' make in statement (2) above? In what case would it make a difference in this context?
Thanks
Harwinder
[ October 24, 2003: Message edited by: Harwinder Bhatia ]
 
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables and parameters must be declared final if they are accessed in an inner class. I discuss the reason why at length in Chapter 1 of Mastering the Fundamentals.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your prompt reply, Doug.
I d/l a copy of your book earlier today after reading some rave reviews from other people on this website. I have only had a chance to browse through the first few pages. But, I must say that it seems to be very comprehensive. I am looking forward to reading it thoroughly in the coming days and will provide feedback later. Thanks a lot for sharing such a great piece of work with us and that too without an associated price tag.
Now, coming back to my question, let me just rephrase it. I understand that the expression
final char c = '\u0001';
in method m1 makes 'c' a compile-time constant but why doesn't the final modifier in the method argument of m3 make the variable 'c' in m3 a compile-time constant too?
Thanks
Harwinder
 
Doug Dunn
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what the practical significance of this is, but a method or constructor parameter--regardless of the fact that it is declared final-- could never be a "compile-time" constant expression for the simple reason that it is not initialized until run time when the method or constructor is invoked.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same problem a while back..
This post might help.
https://coderanch.com/t/243475/java-programmer-SCJP/certification/Dan-char-byte
 
Doug Dunn
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I came back one last time to make sure I didn't leave any mess behind, and I'm glad I did. I see now what is happening in this example, and why the fact the method or constructor parameters could never be inlined is significant. I can tell you I know some very smart programmers who have been compiling lists of the significance of inlined constants (or compile-time constant expressions) for years, and I have never seen this. I have such a list in my book in the section on inlined constants, and will add this example, or a comparable one. A nice parting gift for me (since no one else gave me anything )
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Doug. Your explanation does make sense. I'm getting more n more intrigued by your book as I'm reading it.
Thanks Cathy. I had seen your post but had not paid close attention to the example until yesterday when I was taking the mock exam myself.
Regards
Harwinder
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Doug. Please take note that Dan's mock exams are copyrighted.
[ October 25, 2003: Message edited by: Marlene Miller ]
 
Doug Dunn
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I thought I would escape my week at the ranch without being flamed . Look anyone who has read my work knows that I would never use someone else's material without proper credit. I received special permission to liberally quote from Sun material precisely because of the professional quality citations in my work. In this case, the idea needs to considerably broadened, so much so that I am sure I will address it in the chapter on type conversions with only a cross-reference in the section on inlined constants. Nevertheless, since I have received a private email requesting that I cite this material, as well as this public reminder, I will make a point of citing Dan Chisholm as the source of the idea. Chisholm certainly deserves credit because he is obviously making a huge contribution to Java or else his mock exams would not be so popular on this Web site. In fact, I will be asking him to consider a full technical review of Mastering the Fundamentals, in which case you will find his name plastered all over my book.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doug. I apologize. I did not mean to suggest you would do or even think of something less than professional and honest. We share a lot of good ideas on this forum. As people study they are always coming up with creative questions and useful examples. Often those ideas get lost as they roll off the screen. It's good for everyone when those ideas, or some variation or reincarnation, can be presented to a larger audience.
 
reply
    Bookmark Topic Watch Topic
  • New Topic