-1

I have some doubt when to declare class as final. while I googling about it then I get to know that it will help full for security reason same as string immutable with help of SO POST[1].

As I know final variable initialize once. just example:

final int i=10;

i=11; //give compile time error

becauseThe final local variable i cannot be assigned. It must be blank and not using a compound assignment

But when I created the final class variable multiple time. it is allowing me to do that. if it allow me to create more than one instance of final class then how can I say it's final?

can you give some example of final class through it fulfill some business logic needs?

If I am not wrong than is primitive re-initialization not allow but object allow to create multiple time.

Community
  • 1
  • 1
NFE
  • 1,147
  • 1
  • 9
  • 22
  • 1
    You appear to be confused between final classes and final variables. It would help if you'd show a short but complete example of the behaviour which surprises you. – Jon Skeet Jun 01 '13 at 18:10
  • @JonSkeet thanks sure I will.I mean to ask. can you elaborate more on `behaviour which surprises' – NFE Jun 01 '13 at 18:15
  • Sorry, I don't understand your comment, and it sounds like it would probably be a fairly vague question anyway. – Jon Skeet Jun 01 '13 at 18:16
  • @TBM Can you provide the code that is not working the way you expect it to? – FeifanZ Jun 01 '13 at 22:38

3 Answers3

3

Making a class final only prevents people from extending it. You may still create as many instances as you want.
This is useful for example when you want to make sure that a class is immutable, like String is. If you could extend String, you could add behavior that alters the String, thereby breaking the immutability.
For class instance variables, the rules are the same as for primitives, if they are declared final they can't be reassigned. You can however still change members of the instance.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • thanks. if I prevent some class to be not extendable, through it what I achieved? what can I do control? sorry if it's so stupid question but I never come with such requirement. can you give some example or any ref link. – NFE Jun 01 '13 at 18:11
  • Added a little more explanation. – Keppil Jun 01 '13 at 18:22
1

final means that the class cannot be extended. You can still create multiple instances of the class. For instance:

class ImmutableInt {
  int value;
  public ImmutableInt( int val ) {
    this.value = val;
  }
  public int getValue() {
    return value;
  }
  // no setter because I don't want people to change it!
}

In the above case, I could create many instances of ImmutableInt.

ImmutableInt int1 = new ImmutableInt( 10 );
ImmutableInt int2 = new ImmutableInt( 100 );

Those two instances would be set to values of 10 and 100 always.

Now let's say someone decides they want to be able to change the value of ImmutableInt, so they extend ImmutableInt and provide a setter. Well, that doesn't really make sense for something that is supposed to be immutable

So, I add final to the class to say, "Nobody can extend this class to break my contract of immutability." I also add final to the field so that it can only be initialized in the constructor and not changed afterward.

final class ImmutableInt {
  final int value; // no default value here means it must be set in the constructor

  public ImmutableInt( int val ) {
    this.value = val;
  }

  public int getValue() {
    return value;
  }
  // no setter because I don't want people to change it!
}
digitaljoel
  • 26,265
  • 15
  • 89
  • 115
0

final has different meaning when used at different places so don't mix the concepts mentioned here:

Final classes

A final class cannot be subclassed. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String.

Final variables

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136