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
5 answers

Does it matter whether finals are static or not?

Does it matter whether I declare a class constant as static or not? public class MyClass { private final static int statConst = 1; private final int nonStatConst = 2; } statConst and nonStatConst can never change since they are final, so…
Andreas
  • 7,470
  • 10
  • 51
  • 73
0
votes
5 answers

Static final member variables vs. get methods

I have a class with all the configuration from a property file. My first solution is this: public class Config { public static final int disc; static { // Read property file and set properties disc = 5; } } Reading…
user1883212
  • 7,539
  • 11
  • 46
  • 82
0
votes
4 answers

Java: How to make a final subarray?

Consider the following snippet: public class TestBench { private static final short matrix[][] = new short[][] {new short[] {}}; public static void main (String args[]) { matrix = new short[][] {new short[] {}}; // illegal …
GuiRitter
  • 697
  • 1
  • 11
  • 20
0
votes
3 answers

Access variable from inner class without making it final

how could i get this to work: public void onStart() { super.onStart(); int dayN = 0; int i = 0; String day = null; String addFach; String mo1 = null; …
ntm
  • 731
  • 2
  • 13
  • 37
0
votes
1 answer

Is there a tool for the conversion of duplicate literals into final Strings/magic numbers in Java?

I have some Java files that use the same String values up to 20 times each, and I want a tool that replaces those Strings with a private static final String STRING_TO_CAPS_AS_CONSTANT or something along those lines. If it can also convert numbers…
NobleUplift
  • 5,631
  • 8
  • 45
  • 87
0
votes
4 answers

Duplicate final declaration, should it be avoided?

If we have a private final instance variable to be passed to a private method, do we redeclare it with final modifier in the function when passed as parameter ? eg: public class GraphAlgo { private final source; public GraphAlgo(source) { …
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
2 answers

Inheritance of static final --> not visible

Hey guys I am a total java newbie and to be honest I am not sure on how to explain my problem to you. So I have 2 classes, one of which is inherited by the other. Now I am not allowed to change the superclass, or else this would be much easier. The…
0
votes
1 answer

Defining final in constructor or as a field in java?

public class IdariPersonel extends Personel { final int sicilNo; public IdariPersonel(int idno, int sicilNo){} } move the sicilNo definition in constructor, int final sicilNo, it accepts that too. What is difference?
CursedChico
  • 571
  • 4
  • 7
  • 17
0
votes
2 answers

Thread safety for classes with non-final members with no mutator methods

For the following class, what are the implications of making mNumSides final? Is "thread-safety" affected? class Shape { private int mNumSides; public Shape(int numSides) { mNumSides = numSides; } public int getNumSides()…
Matthew
  • 6,356
  • 9
  • 47
  • 59
0
votes
2 answers

Serialization, Cloning and Final Guarantees (Immutability)

We know that new JMM gives guarantees for not seeing partially constructed object or more than one value of its final fields. http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.2 My question is - Is same final guarantees are…
veritas
  • 2,444
  • 1
  • 21
  • 30
0
votes
2 answers

Final parameters are copied into a hidden variable?

This is copied from JPL (see comment below), I added import and main(): import java.util.*; /** * @From "The Java Programming Language" by Arnold, Gosling, Holmes * (5.3. Local Inner Classes) */ public class LocalInnerClassAppl { public…
TT_ stands with Russia
  • 1,785
  • 3
  • 21
  • 33
0
votes
1 answer

How to mock final instance variable in a class using JMockit

I am trying to use JMockit to do behavior test. In one of the test, I need to call a method in the final object. However, the final object is not initialized (null object) and I don't know how to initialize it in the test (since it is defined…
jgmao
  • 147
  • 2
  • 8
0
votes
1 answer

Passing int value, .putExtra, loop

I need to pass int value i to other class, because it's the id from a database, but it doesn't allow me to pass the int value using .putExtra, because the value needs to be int, but if I declare that value as final then it can't change anymore for…
user1816780
  • 121
  • 4
  • 14
0
votes
4 answers

Double null check in single line

Is the following code the best way to check for null for two times in a single line for initializaing a final variable? final String textValue = text != null ? text.getText() != null ? text.getText() : "" : "";
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
0
votes
2 answers

Changing JButton Text or color without final?

Hey all I can change the text of 1 single button easily with "final" but I need to create lots of buttons for a flight booking system, and when the buttons are more, final doesnt work ... JButton btnBookFlight; eco = new…
Anarkie
  • 657
  • 3
  • 19
  • 46