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
0
votes
2 answers

Unable to access my constant/final String array values declared in a Java interface

So I have an interface, it's got a few constant integers declared and I can access these in any class which implements said interface. I just declared an array of constant strings in the interface, and I tried to access it, resulting in a null…
dahui
  • 2,128
  • 2
  • 21
  • 40
0
votes
1 answer

Making a static field final in Java

Looking at the following code snippet, the scenario given is about fetching a database connection from a database connection pool in the Tomcat Server. public final class DatabaseConnection { private static DataSource dataSource; static { …
Tiny
  • 27,221
  • 105
  • 339
  • 599
0
votes
4 answers

Why can't variables local to a method be declared final?

I like to know why Variables that are local to a method cannot be declared final. Is there any specific reason? Does it mean are there no local constants in Java?
giri
  • 26,773
  • 63
  • 143
  • 176
0
votes
0 answers

Android R.Java Reserved Workd Final disappeared from my variables

i had this code public static final class id { public static final int LinearLayout1=0x7f090007; public static final int LinearLayoutVentas=0x7f09003a; public static final int RelativeLayout1=0x7f090026; public…
angel
  • 4,474
  • 12
  • 57
  • 89
0
votes
2 answers

Sorting a collection of objects in Java where implementing class is final

I'm using a public API provided by a 3rd party and I have a collection of objects that are defined as final. The object has a method getName() which lets me retrieve the String name of the object. What I need to do is sort the objects by name and…
0
votes
1 answer

final static attribute change.. any idea or approach?

I have to maintain a code to add more flexibility to a final static variable in a class. The variable is no more a global constant and may be changed. The problem is that the class is in a common library and used in different projects. Do you have…
Akram GARGOURI
  • 205
  • 1
  • 10
0
votes
4 answers

Some clarification about the variable declared as final in Java?

I have the following doubt about the declaration of a variable as final in Java If I declare the following variable in my code: private final String CURRENT_USER; Eclipse show me the following error message: The blank final field CURRENT_USER may…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
0
votes
3 answers

Final variables accessibility

Why can't Final variables be accessed in a static variables. At the compile time they are simply substituted directly substituted with their values so, they should be allowed to use even in static methods why is this restriction???
Mahesh Gupta
  • 925
  • 2
  • 10
  • 25
0
votes
1 answer

How does Java treat final fields accross instances?

I have two classes using the same String value, and both storing them as a final private field. public class Foo{ private final String str; private final Bar bar; //notice this public Foo(String in){ this.str=in; …
rath
  • 3,655
  • 1
  • 40
  • 53
0
votes
1 answer

Should all classes that aren't inherited be final?

Should I declare all classes that aren't inherited as final even if they don't have anything to do with inheritance and that kind of stuff?
user2176127
0
votes
2 answers

I'm trying to time how long a key is held down using vPython

I'm writing a program for my physics final. Throughout the semester we have used vPython to model situations and get exact answers, etc. Our final project is to create a game using vPython that includes some type of physics. I chose to remake…
0
votes
3 answers

Final variables and methods inside a final class

Are there any benefits to finalizing a class and finalizing each individual variable and method inside that class? Or would that be redundant?
user3055593
  • 69
  • 1
  • 8
0
votes
2 answers

why in java static final variable in interface can be overridden by a local var?

I know static means, there is only one instance of it in the memory. I know final means it can not be changed or subclassed,I also know any variable defined in a java interface is static final so now here is the question , why can I over ride final…
Medya Gh
  • 4,563
  • 5
  • 23
  • 35
0
votes
1 answer

Why should we change the modifiers of fields outside an inner class to final?

I have a question about why should we set a field final when we use it in an innerclass? for example why should we set the modifier of textField to final? My question is that why it will not be available if we do not declare it as final? final…
Siavash
  • 503
  • 1
  • 5
  • 25
0
votes
3 answers

Forcing a subclass to define a protected final instance variable

Sometimes I need a property in the superclass (e.g. ChessFigure) that should force the subclass (e.g. Pawn or Bishop) to implement a const (final) property. For example every Chess figure has its own Image, but it's static (but only static for the…
Luca Nate Mahler
  • 1,292
  • 2
  • 13
  • 28