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

Cloning and serializing a final object

When a class is declared as final, is it possible to clone or serialize it's objects? Or is this impossible because final prevents the extending of subclasses, therefore preventing cloning and serialization from implemented?
user3007194
  • 111
  • 1
  • 8
0
votes
1 answer

even when use constans : case expressions must be constant expressions

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Constants.EXIT : Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); …
Jesus Dimrix
  • 4,378
  • 4
  • 28
  • 62
0
votes
0 answers

Inner method called twice, uses same final object

I have a method in a class that is called roughly at the same time two different objects. public void doSomething(final int i) { if(getId() == i) { System.out.println("outer "+i); Platform.runLater(new Runnable() { …
user1406177
  • 1,328
  • 2
  • 22
  • 36
0
votes
2 answers

Using "this" in the constructor of a final class

I have an inner helper class, and I would like to add each new instance of it to a map in the containing class, like this: public class SomeService { private final Map pendingTasksByKey; private class ServiceTask { …
Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
0
votes
3 answers

How to initialize a final object array

I'm trying to add an array of object in my class(MainActivity), for example public class MainActivity extends Activity { private class A { A(String s) { ..} } private static final A[] aList1; private static final List
Deqing
  • 14,098
  • 15
  • 84
  • 131
0
votes
1 answer

Why are interface variables implicitly static in Java?

I've read posts that said something like: "A java constant is a static final variable" I don't really understand why this is true. Why isn't marking it as final enough? Why do we need to add the "static" modifier? If it's a final field inside an…
matanc1
  • 6,525
  • 6
  • 37
  • 57
0
votes
2 answers

Why does eclipse force me to use a final?

I have a constructor with size as a parameter. Eclipse forces me to declare Integer size as final. Why ? public LRUCache(final Integer size) { lhm = Collections.synchronizedMap(new LinkedHashMap() { …
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
4 answers

Final class usage as variable holder

So im having a problem on my game because i cant figure out how to handle my Explosion ArrayList since i need to add elements to it from several different places, and while searching a solution to this, i came up with a very messy solution which…
Xkynar
  • 935
  • 1
  • 10
  • 31
0
votes
5 answers

Final attribute of a final datatype?

What happens when I declare something like: public static final String forma = "Rectangular"; Isn't it redundant? Being String a final class, what would it change not setting forma as final? For static, I think it is not making any change.
diegoaguilar
  • 8,179
  • 14
  • 80
  • 129
0
votes
1 answer

static final or static use for LogBack Logger?

Should I use a static final or static logger? private static final Logger logger = LoggerFactory.getLogger(MyClass.class); versus private static Logger logger = LoggerFactory.getLogger(MyClass.class); Remark: a simular question is available, but…
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
0
votes
2 answers

Adding a final double to my program

I'm attempting to make a few doubles in my code final. I've tried a few methods such as "public static final," just "final," etc. import java.util.Scanner; import java.text.DecimalFormat; public class Pay_stub { public static void…
Dingles
  • 149
  • 2
  • 3
  • 8
0
votes
3 answers

Another class interacting with variables in main method

I have a main method which creates a List. I then have another class whose state is a List - the goal of this class is to take in the main method's List as its state and do manipulations to it without affecting the main method's…
user2763361
  • 3,789
  • 11
  • 45
  • 81
0
votes
3 answers

Can a nested class inherit from a final enclosing class in Java?

I know a subclass can not inherit from a final superclass but this subclass section of my book never indicates what would happen in the case of a nested class.
Ethan
  • 83
  • 1
  • 2
  • 7
0
votes
3 answers

compareTo based on two values of objects

I'm working on a 'Franchise' program which has a owner, state, and sales, that are all set in the constructor and can't be changed. My problem comes when I'm trying to write my compareTo method. package prob2; public class Franchise implements…
user2745043
  • 189
  • 2
  • 7
  • 24
0
votes
4 answers

Java final instance variable initialized from a method

I need to make a final string in a class and initialize it from within a method, i searched the internet some says its possible some say not! i passed over this and in the accepted answer he stated that's possible from constructor said nothing about…
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56