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

OnItemclick loosing value of string

Good day everyone. Unfortunately i don't have much experience with java and i run into a probably very basic issue. I am filling a list view and when i click one of the list items i want to send a string to another intent. but the value of the…
Maantje
  • 1,781
  • 2
  • 20
  • 33
0
votes
2 answers

In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

For example lets say we have: public void doThis() { final Foo foo = Foo.getInstance(); ... initialize foo somehow... baz(Bar.getInstance(foo)); // adds Bar instance to something ... bar will be popped from some…
Ben
  • 193
  • 1
  • 11
0
votes
5 answers

Is a final array of final strings still mutable?

Suppose I have an array public static final String[] fooArray ={ Foo.a, Foo.b, Foo.c }; where Foo.a b and c are static final Strings. Could I still do something like fooArray[0] = "taco"; and end up with { taco, Foo.b, Foo.c } as the contents of…
Orch
  • 887
  • 2
  • 8
  • 21
0
votes
0 answers

Why we cannot use static keyword inside a method in java?

I tried to create static object and variable inside the method but i cannot. This same operation is possible outside the method. Please help me anyone. Class Sample() { } class Example { void Mark() { static int i=100;…
Albertkaruna
  • 85
  • 2
  • 11
0
votes
2 answers

Accessing "final" fields of a superclass in a subclass (and updating them) in Java

I have a field for Buildings and subclasses of different types of Buildings that inherit from the Buildings class. The Buildings class has a field called "totalNumber" which I want to update every time I add a building in one of my subclasses. I…
0
votes
1 answer

Instance variable thread safe in managed bean in jsf 1.2

We are using JSF 1.2 and WAS 6.1 in our application. I am from servlet background and understand instance variable of a servlet class are not thread safe because instance variable are shared among all requests AND each request creates a new thread…
Sunny
  • 13
  • 2
  • 8
0
votes
4 answers

Cannot reduce the visibility of the inherited method from object

I have defined the following two classes: public abstract class Subject { private ArrayList clockObserverList = new ArrayList(); public void attach(ClockObserver clockObserver) { // begin-user-code …
Alex
  • 303
  • 1
  • 6
  • 17
0
votes
2 answers

Why can I inherit final method from inner class?

I discovered that following code compiles: class Ideone { public static void main (String[] args){ new Ideone().m(); } final private void m(){ System.out.println("private final method"); } class A extends…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
0
votes
4 answers

Can't create a public static final String in java

This code: public class CommandPrompt { public static void main(String[] args) { public static final String prompt = System.getProperty("user.name")+">"; System.out.println(prompt); } } Returns this error…
Jack Pettersson
  • 1,606
  • 4
  • 17
  • 28
0
votes
3 answers

Trying to understand final variables

Final variables. I have read everything I could find on it on this site, and others, and I almost understand them, but I'm still a bit confused. So, I know the official answer for why we would declare a variable as final: it locks the variable and…
craigmiller160
  • 5,751
  • 9
  • 41
  • 75
0
votes
2 answers

Does final keyword in c++ allow for additional compiler optimizations?

I was thinking about virtual calls and how they work. I understand that virtual calls can be inlined and hardcoded at compile time for value types. If the pointer type is of a class declared as final (like sealed in c#) could this allow the compiler…
guitar80
  • 716
  • 6
  • 19
0
votes
1 answer

Usage of the final keyword

Why is it okay to declare final local variables (within methods) without initializing them but not to declare final fields without initializing those? public class VariableUsingFinal { //final int a; it won't take without intialization …
hari m
  • 1,715
  • 2
  • 10
  • 7
0
votes
1 answer

Change background color of button on click and then open a new activity

I am trying to make a Button change its background color and launch a new activity on click. Below is the code I wrote, but I am getting error message saying to declare 'Button btn1' as 'final Button btn1'. If I do that, the button change its color…
denise
  • 29
  • 1
  • 4
0
votes
2 answers

Java: Somehow I managed to extend a final method

The Vector3 class contains this: public final boolean set(float x, float y, float z) { setX(x); setY(y); setZ(z); return true; } The Vector4 class contains this: public boolean set(float x, float y, float z, float w) { setX(x); …
0
votes
2 answers

Error in Button event android

I am doing a project Android to recreate an application similar to Simon, I created all the buttons, and thanks to the toast, I can see what they do. my problem is that I cannot generate a random number to the button to start * I tried to use a…
PiBi
  • 39
  • 1
  • 1