Results 1 to 13 of 13
  1. #1
    Ruben is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Question Access Modifier 'final'

    Hey

    can anyone give me a good example of when the access modifier 'final' could be used when creating a class ?
    I'm aware of what it does and all, but i just can't seem to find an example that makes it clear to me why it should be used for that
    particular class.

    Thanks!

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Access Modifier 'final'

    You can use it for constants, things that don't change. For example

    Java Code:
    private static final double pie = 3.14159;

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Access Modifier 'final'

    can anyone give me a good example of when the access modifier 'final' could be used when creating a class ?
    Please define what you mean.

    • Do you mean marking a class as final (meaning not extendable)?
    • Do you mean creating a final field within a class (meaning unable to change the value)?
    • Do you mean creating an instance of a class that you cannot reassign?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Access Modifier 'final'

    • Do you mean creating a method that a subclass cannot override?


    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Ruben is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Access Modifier 'final'

    No, I'm not talking about declaring variables as final and also not about creating methods dat can't be override by subclasses.
    I'm talking about marking a class final (meaning that it is not extendable) like doWhile said.

    Like I said, I know what it does, but i can't think of an example of when I would use it in 'real life' when programming...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: Access Modifier 'final'

    Look at String.
    It's an immutable class (that is you cannot change the data in it once it's been created).
    Now, if that class were not final then someone could create an extended version of String that allowed the data in it to be changed, utterly breaking the concept of an immutable String.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Ruben is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Access Modifier 'final'

    Say you make a string object:
    String alphabet = "abc";

    when applying a method on this String object from the String class,
    wouldn't you be changing the data in this object ?

    or does it just return a new object from the String type ?

    e.g : aphabet.toUpperCase();

  8. #8
    Jeeva7 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Access Modifier 'final'

    Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

    Note that you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

    Explanation:
    in case if string class is not immutable then

    class MyString extends String {
    public boolean equals(Object anObject) {
    return true;// my equals method
    }
    }

    String string = new MyString();
    string.equals(anotherString); // this will give you unexpected results because string object will take the overridden method

    if your class is immutable and you don't want your class to be extended/ overridden then you can use final class.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Access Modifier 'final'

    I dislike one particular use of final: local variables need to be final if used in nested (local) classes; I know and understand that these local variables need to be copied to instances of the nested local class but the reason that they have to be final is beyond me. One reason I can come up with is that if the method body is still in scope while at least one instance of a local nested class is present, those multiple copies of that variable have to be synchronized. But that is nothing a little hacking or redefinition can't solve ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: Access Modifier 'final'

    Quote Originally Posted by Ruben View Post
    Say you make a string object:
    String alphabet = "abc";

    when applying a method on this String object from the String class,
    wouldn't you be changing the data in this object ?

    or does it just return a new object from the String type ?

    e.g : aphabet.toUpperCase();
    Try it:

    Java Code:
    public class TestCase {
       public static void main(String args[]) {
          String lowercase = "lowercase";
          String uppercase = lowercase.toUpperCase();
          System.out.println("lowercase = " + lowercase);
          System.out.println("uppercase = " + uppercase);
       }
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Access Modifier 'final'

    Quote Originally Posted by Ruben View Post
    No, I'm not talking about declaring variables as final and also not about creating methods dat can't be override by subclasses.
    I'm talking about marking a class final (meaning that it is not extendable) like doWhile said.

    Like I said, I know what it does, but i can't think of an example of when I would use it in 'real life' when programming...
    Declaring an entire class as final is helpful in defining the class as immutable. In doing so, you create security in certain contexts (for instance multi-threading). See Immutable Objects (The Java™ Tutorials > Essential Classes > Concurrency) . You also provide advantages when it comes to garbage collection (see Java theory and practice: Garbage collection and performance ). String, as well as the primitive wrapper classes (Integer, Long, etc..) are all examples of this.

  12. #12
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Access Modifier 'final'

    "I'm talking about marking a class final (meaning that it is not extendable) like doWhile said."


    Make the constructor of this class be private . That is a little trick .


    Ksharp

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Access Modifier 'final'

    Quote Originally Posted by Ksharp View Post
    "I'm talking about marking a class final (meaning that it is not extendable) like doWhile said."


    Make the constructor of this class be private . That is a little trick .
    That's not just making it a final class; that is locking the door for everyone except for that class itself. Singletons and utility classes use this idiom when they want one or no instances of the class. Marking the class as final is enough to make it a final class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Protected access modifier in Java
    By bluepratz in forum New To Java
    Replies: 6
    Last Post: 08-04-2010, 10:08 AM
  2. access modifier in constructor
    By javaPower in forum New To Java
    Replies: 1
    Last Post: 06-22-2010, 09:56 AM
  3. default access modifier
    By vishnugch in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 12:48 PM
  4. Regarding default access modifier?
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 03-14-2009, 08:21 AM
  5. why we use public access modifier?
    By vichet in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 07:04 AM

Tags for this Thread

Posting Permissions

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