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

Entity classes and final keyword

From another thread By definition, an entity is a class that can be persisted to a database, so having a final field would make no sense in the context of something that will end up being a record in your database. Could someone elaborate on…
xrdty
  • 886
  • 2
  • 10
  • 22
0
votes
2 answers

Java: the final local variable cannot be assigned, since it was defined in an enclosing type

How may one fix this problem? The error lies @ scrollPane and map. scrollPane error: the final local variable cannot be assigned, since it was defined in an enclosing type map error suggestion: initialise variable. final Map
TaiAu
  • 49
  • 6
0
votes
3 answers

How to assign value to a final variable

the variable "startTs" needs to be declared as final. and when I declare it as final android studio highlights it and says "cant assign value to final variable" and if removed the final modifier i receive "variable must be final" now i want to use…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
0
votes
0 answers

Final variable casting in Java

final double integer = 20/100; If i was to print integer, I would get 0 rather than 0.2 double integer1 = 20/100; If I was to print integer1, I would get 0.2 final double integer3 = 200/100; If I was to print integer3 out, I would get 2.0. Can…
Olympus
  • 105
  • 1
  • 6
0
votes
2 answers

Location of static final constants for method in Java class

Let's say I have a class with a method that uses some static final variables as constants, but no other method in the class uses them. For example, an AVLTree class with a balance method that uses these constants to describe balance factors for…
Josh Broadhurst
  • 407
  • 3
  • 14
0
votes
2 answers

Cannot assign value to final Volley variable response

I am familiar with Volley and creating a Singleton class, and adding requests to the queue. However, I wish to increase the modularity of volley and simply call all requests through method call to another class instead. I have setup the present…
Sauron
  • 6,399
  • 14
  • 71
  • 136
0
votes
1 answer

JMH static final field as parameter

I see in JMH a popular problem with ConstantFold, but what if I have inverse problem. I need the static final field as parameter. For example, it's can be some constant variable for some algorithm. But in java-doc I see: {@link Param} fields should…
Ilya K.
  • 241
  • 2
  • 9
0
votes
1 answer

Android Changing a TextView within a onClickListener requires a final declaration?

So I'm trying to get the user to be able to set an onClickListener on a button which will change a TextView upon clicking. However, this causes a final declaration error to appear because I am calling it from an inner class. I don't understand why…
jakeinmn
  • 167
  • 1
  • 3
  • 13
0
votes
2 answers

What is the actual benefit of immutable objects like String instead of using just static final?

I mean, why do defacto immutable objects exists? Why do we not just use the final static modifiers? What is so important about String that Java makes it immutable?
0
votes
1 answer

Why is the System class declared as "final" in Java?

As per my understanding, a class is declared as final to prevent it from being extended/inherited. So I see there can be security and probably some performance gains in this regard. But is there a very specific design decision behind this? Say for…
0
votes
1 answer

Class extends BufferedImage, but picture can't change. Any way around this?

I'm trying to create a simple Card class that extends BufferedImage so I can draw the card directly on the screen. But the card has two faces. The front, and the back. I am including a flip(boolean faceup) method that I want to change the image from…
user3376587
  • 134
  • 2
  • 12
0
votes
1 answer

Why can't I initialize my double constant DOSE with scanner input?

I'm a beginning java programmer. I'm trying to write a simple program to calculate how much of a substance remains in your body based on the half-life of the substance, and when equilibrium will be reached based on when and how the medication is…
fischbs
  • 55
  • 1
  • 2
  • 8
0
votes
3 answers

Need to initialize a static final field in the subclasses

I asked a question, but it was very dirty, and a lot of people didn't understand. So, I need to declare a final static field, that is only going to be initialized in the subclasses. I'll show an example: public class Job { public static final…
0
votes
0 answers

How performance is enhanced by using final() methods in java

I was reading through Java-The Complete Reference,and then I encountered this statement which says that- Methods declared as final can sometimes provide a performance enhancement: Reason given is- The compiler is free to inline calls to them…
Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42
0
votes
0 answers

Can anyone explain why should we finalize an object?

private static final SecureRandom no = new SecureRandom(); Why should we finalize no in here ??