Results 1 to 10 of 10
  1. #1
    sumanandjesus is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Arrow what is the use of final keyword

    hi

    can anyone frwd me the use of final keyword in core java iam new to java and iam in learning stage help with great heart


    thanks

  2. #2
    sumanandjesus is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    hi

    can anyone frwd me the use of final keyword in core java iam new to java and iam in learning stage help with great heart


    thanks

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It can not be changed after initialization. The reason you want that, is to simplify maintenance. If you declare it final, no one can muck with it, including subclasses or other engineers who maintain the code.

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    on a class, it prevents subclassing
    on a field, it prevents instantiation without initialising the field (to a constant value)
    on a local variable, it declares it to be constant, allowing references from inner classes and various compiler optimisations

  6. #6
    arseny is offline Member
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    There is one more to the OrangeDoc comment. Some will get an error at the compilation stage when he will try to break your constant restriction.

  7. #7
    mrbharatmehta is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    Hi Folks,
    Just wish to add one point to ones given by our fellow OrangeDog here:

    final if used against a method in a class prevents any subclass of that class from over riding that method

  8. #8
    melaka is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default

    You can use final key word for classes methods and variables. When it is used methods cannot be overridden, classes cannot be extended and variables cannot be reassigned.

  9. #9
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    So obviously, the main idea is that, depending on context, "final" essentially means "cannot be overridden/extended" and "cannot be modified once set". A couple of details that are sometimes overlooked:

    - subtly, final when used with an instance or class variable also means must be set by the time the constructor/initialiser exits-- this isn't necessarily implied by just saying "can't be modified once set".
    - final used with local variables (ones you declare inside a method) also allows you to make variables visible to anonymous inner classes where they otherwise wouldn't be (this is essentially a compiler trick)
    - declaring an instance/class variable final interacts with the Java memory model: it guarantees that the value set on that variable during construction/initialisation will be visible to other threads as soon as the object/class reference is visible. (Otherwise, and without other synchronization, this guarantee isn't made.)

  10. #10
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    Final keyword when used for variables:

    u cannot initialize variable more than once.

    when used for class:

    u cannot subclass that class

    when used for method:

    u cannot overrride final method.

Similar Threads

  1. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM
  2. split Keyword
    By santhoshrao in forum New To Java
    Replies: 4
    Last Post: 08-13-2008, 10:28 AM
  3. How to use Switch keyword
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:07 PM
  4. Method with final keyword
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-29-2007, 09:39 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •