final

Why do variables passed to runnable need to be final?

旧时模样 提交于 2019-11-26 17:00:55
问题 If I have a variable int x = 1 , say, and I declare a runnable in the main thread, and I want to pass x to the runnable's run() method, it must be declared final . Why? final int x = 0;//<----must be final... private class myRun implements Runnable { @Override public void run() { x++;// } } 回答1: Because if they are able to be changed, it could cause a lot of problems, consider this: public void count() { int x; new Thread(new Runnable() { public void run() { while(x < 100) { x++; try { Thread

Anonymous-Inner classes showing unwanted modifier

别等时光非礼了梦想. 提交于 2019-11-26 16:57:59
问题 To my understanding, the following code should have printed true . However, when I ran this code it is printing false . From Java docs of Anonymous Classes 15.9.5. : An anonymous class is always implicitly final public class Test { public static void main(String args[]) { Object o = new Object() { }; System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers())); } } Can some one please help me understand this behavior. 回答1: Note that the wording in the JLS

Initialize final variable before constructor in Java

独自空忆成欢 提交于 2019-11-26 16:46:39
问题 Is there a solution to use a final variable in a Java constructor? The problem is that if I initialize a final field like: private final String name = "a name"; then I cannot use it in the constructor. Java first runs the constructor and then the fields. Is there a solution that allows me to access the final field in the constructor? 回答1: I do not really understand your question. That public class Test3 { private final String test = "test123"; public Test3() { System.out.println("Test = "

Why isn't a qualified static final variable allowed in a static initialization block?

天大地大妈咪最大 提交于 2019-11-26 16:13:29
Case 1 class Program { static final int var; static { Program.var = 8; // Compilation error } public static void main(String[] args) { int i; i = Program.var; System.out.println(Program.var); } } Case 2 class Program { static final int var; static { var = 8; //OK } public static void main(String[] args) { System.out.println(Program.var); } } Why does Case 1 cause a compilation error? The JLS holds the answer (note the bold statement): Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. Such an assignment is

How to handle a static final field initializer that throws checked exception

家住魔仙堡 提交于 2019-11-26 16:04:49
问题 I am facing a use case where I would like to declare a static final field with an initializer statement that is declared to throw a checked exception. Typically, it'd look like this: public static final ObjectName OBJECT_NAME = new ObjectName("foo:type=bar"); The issue I have here is that the ObjectName constructor may throw various checked exceptions, which I don't care about (because I'd know my name is valid, and it's allright if it miserably crashes in case it's not). The java compiler

Compile-time constants and variables

左心房为你撑大大i 提交于 2019-11-26 15:51:10
The Java language documentation says: If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a compile-time constant. My understanding is if we have a piece of code: private final int x = 10; Then, the compiler will replace every occurrence of x in the code with literal 10 . But suppose the constant is initialized at run-time: private final int x = getX(); // here getX() returns an integer value at run-time. Will there be any performance drop (howsoever

Behaviour of final static method

半腔热情 提交于 2019-11-26 15:46:47
I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance. So if I have the below snippet, it compiles fine //Snippet 1 - Compiles fine public class A { static void ts() { } } class B extends A { static void ts() { } } But if I include final modifier to static method in class A, then compilation fails ts() in B cannot override ts() in A; overridden method is static final . Why is this happening when static method cannot be overridden at all? NawaMan

Why declare a function argument to be final?

怎甘沉沦 提交于 2019-11-26 15:43:33
问题 I'm currently working my way through the book "Teach Yourself Android Application Development in 24 Hours" published by Sams. I'm relatively new to Java, Android or otherwise. I have a very solid background in ActionScript 3, which has enough similarities with Java that the language itself isn't hard to grasp, but I do still have some questions about the rationale behind some of the code samples in the book. For example, here's a function that comes with the sample code for Hour 9: private

How does “final int i” work inside of a Java for loop?

别来无恙 提交于 2019-11-26 15:37:04
问题 I was surprised to see that the following Java code snippet compiled and ran: for(final int i : listOfNumbers) { System.out.println(i); } where listOfNumbers is an array of integers. I thought final declarations got assigned only once. Is the compiler creating an Integer object and changing what it references? 回答1: Imagine that shorthand looks a lot like this: for (Iterator<Integer> iter = listOfNumbers.iterator(); iter.hasNext(); ) { final int i = iter.next(); { System.out.println(i); } }

Must all properties of an immutable object be final?

左心房为你撑大大i 提交于 2019-11-26 15:19:24
Must immutable objects have all properties be final ? According to me not. But I don't know, whether I am right. assylias The main difference between an immutable object (all properties final) and an effectively immutable object (properties aren't final but can't be changed) is safe publication. You can safely publish an immutable object in a multi threaded context without having to worry about adding synchronization, thanks to the guarantees provided by the Java Memory Model for final fields : final fields also allow programmers to implement thread-safe immutable objects without