Questions tagged [final]

final is a common keyword specifying that the reference declared as final cannot be modified once it is initialized. In Java the keyword final is roughly equivalent to const in C++. In C++ the final keyword allows you to declare a virtual method, override it N times and then mandate that 'this can no longer be overridden'.

Refer to how the final keyword is used in different programing languages

1662 questions
39
votes
4 answers

why are java constants declared static?

Why are Java constants declared static? class Foo { static final int FII = 2 ; } In this example I understand the use of final, But why does it have to be static? Why should it be a class variable and not an instance variable?
Vaibhav
  • 435
  • 1
  • 7
  • 11
38
votes
3 answers

How downcasting takes place by using the 'final' keyword in Java

Consider: class UnderstandingConversion { public static void main(String[] args) { int a=10, b=20; byte c = (a>b) ? 40 : 50; System.out.println(c); // output : lossy conversion error } } This code is…
Yash Deole
  • 515
  • 4
  • 8
38
votes
5 answers

Java : in what order are static final fields initialized?

Okay, so say I have a class that looks like this : public class SignupServlet extends HttpServlet { private static final Logger SERVLET_LOGGER=COMPANYLog.open(SignupServlet.class); private static final ExceptionMessageHandler handler = new…
sangfroid
  • 3,733
  • 11
  • 38
  • 42
38
votes
7 answers

Quick Java question about private static final keywords for fields

I'm declaring a field: private static final String filename = "filename.txt"; First, does the order of private static final matter? If not, is there a standard accepted sequence or convention? Second, the filename in my application is fixed. Is…
Spencer
  • 4,018
  • 10
  • 33
  • 43
37
votes
3 answers

Checking for existence of C++ member function, possibly protected

I'm trying to detect whether a class has a particular function (specifically shared_from_this(), which is inherited from std::enable_shared_from_this). To make things more complicated, I need to know whether it has this function…
Azoth
  • 1,652
  • 16
  • 24
37
votes
6 answers

The final local variable cannot be assigned

I have an array of seats, and the array has two strings(selected and empty). On mouse click, I want to traverse the array and find the selected seat. When I press the button it says: The final local variable seatno cannot be assigned, since it is…
user1326088
36
votes
5 answers

How can I initialize a mixin's immutable data in Dart?

I am programming in Flutter using Dart 2.1.0, and come across this situation: mixin Salt { final int pinches; // Immutable, and I want to delay initialization. // Cannot declare constructors for mixin } class Meat with Salt { Meat(int…
Nick Lee
  • 5,639
  • 3
  • 27
  • 35
36
votes
6 answers

What are the benefits of using identical String literals instead of a final variable?

I've come across a class that includes multiple uses of a string literal, "foo". What I'd like to know, is what are the benefits and impact (in terms of object creation, memory usage and speed) of using this approach instead of declaring the String…
Michael
  • 7,348
  • 10
  • 49
  • 86
36
votes
8 answers

Use of final local variables in java

I was wondering is there any usability of using final local variables. Variables are not overridden anyway when inheritance comes into picture. For example a simple code as below public static void main(String args[]) { final String data =…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
36
votes
7 answers

Declaring a List field with the final keyword

If I have the following statement within a class where Synapse is an abstract type: private final List synapses; Does final allow me to still be able to change the state of the Synapse objects in the List, but prevent me from adding new…
letter Q
  • 14,735
  • 33
  • 79
  • 118
35
votes
3 answers

Why is a `val` inside an `object` not automatically final?

What is the reason for vals not (?) being automatically final in singleton objects? E.g. object NonFinal { val a = 0 val b = 1 def test(i: Int) = (i: @annotation.switch) match { case `a` => true case `b` => false …
0__
  • 66,707
  • 21
  • 171
  • 266
34
votes
6 answers

Why can final constants in Java be overridden?

Consider the following interface in Java: public interface I { public final String KEY = "a"; } And the following class: public class A implements I { public String KEY = "b"; public String getKey() { return KEY; } } Why…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
34
votes
8 answers

Is it a good practice to declare variables "final" wherever possible?

Possible Duplicate: When should one use final? I tend to declare all variables final unless necessary. I consider this to be a good practice because it allows the compiler to check that the identifier is used as I expect (e.g. it is not mutated).…
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
33
votes
5 answers

Why are certain variables marked as final in flutter custom classes?

I noticed in some of the examples online that classes that extend StatefulWidget have instance variables marked with final. Why is that? I understand what the final keyword does. I do not understand why it is being declared with each instance…
MistyD
  • 16,373
  • 40
  • 138
  • 240
32
votes
5 answers

final variable in methods in Java

Possible Duplicate: Why would one mark local variables and method parameters as “final” in Java? I was checking some Java code, I am not good at java at least have some knowledge what final does such as sealed classes, readonly fields and…
Tarik
  • 79,711
  • 83
  • 236
  • 349