final

About local final variables in Java

依然范特西╮ 提交于 2019-12-03 21:57:05
In java Program, parameters which is defined as String in method declaration. But in method definition it is accessed as final String variable. Whether it'll lead to some issues (like security, memory problem)? For Example: Method Declaration join(String a,String b); Method definition public void join(final String a,final String b) { Authenticator au = new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(a,b)} }; } Please help for me and clarify my doubts. Thanks in advance P.S. I'm accessing a and b as final variable because I've

Why can final constants in Java be overridden?

若如初见. 提交于 2019-12-03 18:57:15
问题 Consider the following interface in Java: public interface I { public final String KEY = "a"; } And the following class: public class A implements I { public String KEY = "b"; public String getKey() { return KEY; } } Why is it possible for class A to come along and override interface I's final constant? Try for yourself: A a = new A(); String s = a.getKey(); // returns "b"!!! 回答1: Despite the fact that you are shadowing the variable it's quite interesting to know that you can change final

Can an abstract class have a final method?

旧时模样 提交于 2019-12-03 17:14:05
Can an abstract class have a final method in Java? Sure. Take a look at the Template method pattern for an example. abstract class Game { protected int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); abstract void printWinner(); /* A template method : */ final void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()) { makePlay(j); j = (j + 1) % playersCount; } printWinner(); } } Classes that extend Game would still need to implement all abstract methods, but they'd be

Declaring a List field with the final keyword

若如初见. 提交于 2019-12-03 17:07:24
问题 If I have the following statement within a class where Synapse is an abstract type: private final List<Synapse> synapses; Does final allow me to still be able to change the state of the Synapse objects in the List , but prevent me from adding new Synapse objects to the list? If I am wrong, could you please explain what final is doing and when I should be using the keyword final instead. 回答1: No, the final keyword does not make the list, or its contents immutable. If you want an immutable List

Why won't declaring an array final make it immutable in Java?

丶灬走出姿态 提交于 2019-12-03 15:48:46
问题 Why won't declaring an array final make it immutable in Java? Doesn't declaring something final mean it can't be changed? From question related to immutable array it's clear that declaring an array final doesn't make it unchangeable. The following is possible. final int[] array = new int[] {0, 1, 2, 3}; array[0] = 42; My question is: What is the function of declaring final here then? 回答1: final is only about the reference that is marked by it; there is no such thing as an immutable array in

java basics about final keyword

£可爱£侵袭症+ 提交于 2019-12-03 15:46:48
Can final keyword be used for a method? Absolutely! The final keyword can be applied to just about anything, in each case meaning "you don't get to change this anymore." Here's what it means when applied to... a variable : You simply cannot assign the variable a new value (rendering it a constant , of course) a method : You cannot re-implement (i.e., override) this method in a subclass a class : You cannot define a subclass In each case we're simply indicating: once this thing is declared, this is the last value (or implementation) you'll ever see for it. Yes, it is possible to declare a

javac treating static final differently based on assignment method

余生长醉 提交于 2019-12-03 15:37:31
When I compile: public static final boolean FOO = false; public static final void fooTest() { if (FOO) { System.out.println("gg"); } } I get an empty method fooTest() {} . However when I compile: static boolean isBar = false; public static final boolean BAR = isBar; public static final void fooTest() { if (BAR) { System.out.println("gg"); } } the if statement is included in the compiled class file. Does this mean there are two different "types" of static final in java, or is this just a compiler optimization? In the first case, the compiler does an optimization. It knows Foo will always be

local variable is accessed within inner class (java)

牧云@^-^@ 提交于 2019-12-03 15:22:45
问题 I got two errors after I compiled my code. The errors are: 1. local variable input is accessed within inner class; needs to be declared final String name = input.getText(); 2. local variable c_age is accessed within inner class; needs to be declared final Object child_age = c_age.getSelectedItem(); This is my code: import javax.swing.*; import java.awt.event.*; public class GUI { public static void main(String[] args) { JFrame frame = new JFrame("Try GUI"); JLabel l1 = new JLabel("Please

Should be logger always final and static?

不想你离开。 提交于 2019-12-03 15:18:12
问题 Class can be accessed from many threads. Must be logger in this case also be final and static? Thanks. 回答1: All major java logging packages ( java.util.logging , log4j , etc.) are synchronized and thread safe. The standard pattern of a private final static logger per class is fine even if the class is called from multiple threads. 回答2: Yes the logger should be static and final. Also preferably private. There needs be only one logger instance per class and also unless you are going to change

Final Fields Semantics in Threads

馋奶兔 提交于 2019-12-03 15:11:07
This is from JLS 17.5: The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are. The discussion