-1

I see some codes that have method implementation as

public void setVariables(final int value){
this.value=value;
}

why is the final keyword used in the parameter. why not declare

public void setVariables(int value){
this.value=value;
}

I understand that for classes defined inside methods that access method parameters, they have to declared final

what exactly is the benefit of having final keyword in parameter ?

eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
  • 1
    What does `final` do when applied to variables? That's your answer. – Sotirios Delimanolis Mar 04 '14 at 20:45
  • I think the question here really is "why do people mark the argument as final when it clearly isn't needed?" and the answer might be "that's just what their IDE template for setters generated" – matt b Mar 04 '14 at 20:46
  • 2
    Some people use `final` because most of the time a method's parameter value should not be changed inside the method, and `final` enforces that. Other people believe that including `final` on every method parameter introduces unnecessary clutter. – GriffeyDog Mar 04 '14 at 20:53
  • I think that it is bad idea to pass a constant. It should be public and visible almost to all related classes. If you want to restrict to change variable - simply restrict setters access that's it. In other hand, I mostly use final then I am using anonymous classes and try to pass a variable inside this class method. :) – solvator Mar 04 '14 at 21:01
  • @solvator there's no such thing as "passing a constant" in Java; this doesn't do what you think; it's not like `const` in c++ where it says you aren't going to mutate an object. It simply prevents assigning a new value (primitive or reference value) to the variable inside the method. – Brian Roach Mar 04 '14 at 21:05

2 Answers2

4

Basically, the difference is between

public int doThing( int value )
{
     value = value*2; // OK
     return value;
}

and

public int doThing( final int value )
{
     value = value*2; // Not OK
     return value;
}

This can be helpful to you as a programmer to prevent you from changing the value accidentally.

There is one situation where the final keyword is necessary, and that is if you want to use the value in anonymous nested classes, e.g:

public Object makeThing( final String name )
{
    return new Object()
        {
            @Override
            public String toString(){
                return name; // Won't work if `name` is not `final`.
            }
        };
}

Related:

Community
  • 1
  • 1
trutheality
  • 23,114
  • 6
  • 54
  • 68
  • +1, moreover if you pass a reference, you make it unmodifiable I believe – poitevinpm Mar 04 '14 at 20:53
  • @poitevinpm Using `final` on a reference doesn't prevent using the reference to modify the object to which it points. – GriffeyDog Mar 04 '14 at 20:56
  • 1
    @poitevinpm If you pass a reference, you make it impossible to reassign it to another object, but if the object is mutable, you can still change it (e.g. if it is a list, you can still add elements to it). – trutheality Mar 04 '14 at 20:56
  • This is the correct answer but you have `void` method returning an `int` in your example (just to pick nits). – Brian Roach Mar 04 '14 at 20:59
  • I should have known that this is a super-duplicate before answering :/ – trutheality Mar 04 '14 at 21:09
  • actually, there are no examples provided in the duplicate link, I probably got the message faster here than reading that. – eagertoLearn Mar 04 '14 at 21:31
1

to make sure that you don't override that argument

for example to avoid something like this

public void setVariables(final int value){
  value = 1;
}
jmj
  • 237,923
  • 42
  • 401
  • 438