ajsie
ajsie

Reputation: 79686

point of having an instance variable as final?

what is the point of having an instance variable as final?

isn´t it better to have that variable set as a static final variable then?

cause if it can't be changed by any of the objects, then it's the same as a class (static) variable, right?

Upvotes: 16

Views: 18246

Answers (6)

Ondra Žižka
Ondra Žižka

Reputation: 46806

Two reasons:

1) From the class design view, it allows a programmer to rely on the fact that the field will not change since instantiation - so called "immutable object" (where applied to class members, not the object's reference). Java tutorial says:

Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state.

Immutable objects are a cornerstone of various programming styles, e.g. pure functional programming.

2) The second reasons is JVM optimizations. If all fields are final, then JVM knows the object's state can't be changed, and thus it can make many optimizations, e.g. ommiting thread safety checks etc.

Upvotes: 6

xiaokaoy
xiaokaoy

Reputation: 1696

class A{
    public final int x;

    A(int arg){
        x = arg;  // This is legal  !!!
    }
}

public class HelloWorld{

     public static void main(String []args){
         A  a = new A(1);
         A  b = new A(2);
         System.out.printf("a.x = %d, b.x = %d", a.x, b.x);
     }
}

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

I guess you are thinking about simple case such as:

 private final int num = 3;

That can be better written as:

 private static final int NUM = 3;

However, often you will have references to objects that are mutable, and therefore cannot be shared between classes:

 private final List<Node> children = new ArrayList<Children>();

Or perhaps, a value passed into or derived in the constructors:

 public final class MyThing {
      private final String name;
      public MyThing(String name) {
          this.name = name;
      }
      [...]
 }

Note: final fields can be assigned in constructors (or instance initialiser), not just as part of the declaration.

Upvotes: 5

Roy Tang
Roy Tang

Reputation: 5761

You can assign it a value that is specific to the object instance, which you can't do with a static class variable.

Upvotes: 2

Carl Manaster
Carl Manaster

Reputation: 40336

Nope. static means it's the same across all instances of the class. final means it's not assignable after its initial assignment. So two instances could have different values for a non-static final variable.

There are many reasons you might want to make a variable final; one of the best is clarity. If I read a method and notice that the foo is final, I don't have to worry about where it's changing down below - because it isn't; it can't. I can make more changes to code with final variables with less concern ("did I change the value of foo before or after bar, and does it matter?") because I know that some variables aren't subject to change. It also focuses my attention on the variables that are subject to change - and they're the ones that deserve more attention.

Upvotes: 32

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

It prevents other programmers from doing stupid things that they shouldn't try doing anyway...

Upvotes: 1

Related Questions