Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Final Keyword In Java

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    India
    Posts
    20
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Final Keyword In Java

    I'm not sure where the final keyword comes in handy when used on method arguments.

    If we ignore the use of anonymous classes, readability, and intent statement, it appears nearly worthless to me.

    public void testNullify() {
        Collection<Integer> c  = new ArrayList<Integer>();      
        nullify(c);
        assertNotNull(c);       
        final Collection<Integer> c1 = c;
        assertTrue(c1.equals(c));
        change(c);
        assertTrue(c1.equals(c));
    }
     
    private void change(Collection<Integer> c) {
        c = new ArrayList<Integer>();
    }
     
    public void nullify(Collection<?> t) {
        t = null;
    }

    Enforcing the consistency of some data is not as robust as it appears.

    If the parameter is a primitive, it has no effect because it is supplied to the method as a value and modifying it has no effect beyond the scope.

    If we pass a parameter by reference, the reference is a local variable, and changing the reference from within the method, as described in the scaler topic, has no effect outside of the method scope.

    Consider the following simple test example. This test passes despite the fact that the method changes the value of the reference passed to it, which has no effect.

  2. #2
    Junior Member
    Join Date
    Apr 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Final Keyword In Java

    On the one hand, it prevents nasty surprises by locking things down, like putting a vault on your code's valuables. Keeps everything safe and sound.

    But on the other hand, it can feel a bit like putting training wheels on your creativity, especially for us fresh-faced programmers. What if you write something "final" and then later realize you need to tweak it? Stuck in code purgatory!

    Maybe there's a happy medium here. Like using "final" for core functionalities, the bedrock of your program, but leaving wiggle room for other parts to evolve. It's all about striking a balance between stability and flexibility, like a tightrope walker with a net – gotta have that safety net in case things get wobbly!

    Speaking of safety nets, anyone out there braving the freelance web dev world? This discussion reminded me of https://huntly.ai/employers/developers/web/javascript/, a platform for remote coding gigs. Seems like a good way to test the waters without diving headfirst.
    Last edited by ChristineYoung; April 25th, 2024 at 05:56 AM.

  3. #3
    Junior Member
    Join Date
    Oct 2024
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Final Keyword In Java

    its immutable in the current scope. If you find that you want to change the value of it in a sub-method, then you've probably already taken a wrong turn somewhere.

Similar Threads

  1. What is the use of enum keyword in java
    By vilkas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 26th, 2012, 12:53 PM
  2. What is the use of enum keyword in java
    By vilkas in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2012, 10:34 AM
  3. Use of final keyword
    By vilkas in forum Exceptions
    Replies: 5
    Last Post: December 17th, 2012, 03:17 AM
  4. [SOLVED] Adding the "final" keyword makes difference, I'm confused with the sequence flow.
    By SmokyBrain in forum Object Oriented Programming
    Replies: 2
    Last Post: April 30th, 2012, 01:50 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM