final

Javac missing optimization for effective final

百般思念 提交于 2021-02-19 01:36:30
问题 Fact: javac is programmed to detect if a variable is final or if it can be treated as effectively final . Proof: This code illustrates this. public static void finalCheck() { String str1 = "hello"; Runnable r = () -> { str1 = "hello"; }; } This fails to compile because compiler is able to detect String reference str1 is being re-assigned in function. Now Situation 1: Javac does great optimization for final String instances by avoiding to create StringBuilder and related operations. Proof This

Variable capture in Lambda

无人久伴 提交于 2021-02-16 15:44:04
问题 I can't think why the captured variables are final or effectively final in lambda expressions. I looked over this question and really quite didn't get the answer. What is this variable capture? As I searched solutions for my problem, I read that these variables are final because of concurrency problems. But for such situation why can't we lock the task code in the lambda with a reentrant lock object. public class Lambda { private int instance=0; public void m(int i,String s,Integer integer

Why was 'effectively final' introduced in Java 8? [duplicate]

…衆ロ難τιáo~ 提交于 2021-02-11 17:51:27
问题 This question already has answers here : What is the benefit being able to use effectively final variables in Java 8 lambda expressions (2 answers) Closed 6 years ago . The difference of a local variable being final or effectively final has been discussed here. I do not really understand though, why it was introduced in Java 8. To me it seems like it just gives the programmer the freedom to leave out the final keyword, but treating the variable effectively as final. No change in logic, just a

Why is volatile keyword not allowed for local variables?

梦想与她 提交于 2021-02-07 06:51:17
问题 Consider the snippet: If in a main thread, I have this inside a method - volatile CountDownLatch latch = new CountDownLatch(3); new Thread(new ProcessThread("Worker1",latch, 20000)).start();//20 secs new Thread(new ProcessThread("Worker2",latch, 60000)).start();//60 secs new Thread(new ProcessThread("Worker3",latch, 40000)).start();//40 secs I see that volatile is shown as an illegal modifier. And only final is permitted. And final guarantees initialization safety . public static class

Java final fields: is “taint” behavior possible with the current JLS

左心房为你撑大大i 提交于 2021-02-07 05:27:28
问题 I'm currently trying to understand this JLS section on final fields. To understand the text in the JLS better I'm also reading The Java Memory Model by Jeremy Manson (one of creators of the JMM). The paper contains the example that got me interested: if an object o with final fields is made visible to another thread t twice: first "improperly" before o 's constructor finishes next "properly" after o 's constructor finishes then t can see semi-constructed o even when it is accessed only via a

Why does the compiler deny access to non-final variables inside a lambda [duplicate]

隐身守侯 提交于 2021-01-29 02:59:34
问题 This question already has answers here : Why are only final variables accessible in anonymous class? (15 answers) Closed 3 years ago . I just saw this question, and apparently it's obvious that Java should deny access to non-final variables inside the body of a lambda expression. Why? Edit: for example, I don't see why the following code is harmful: String[] numbers = new String[10]; // put some numerical strings in BigInteger sum = new BigInteger("0"); numbers.forEach(n -> sum = sum.add(new

Why Java don't force to use final with enum properties

我们两清 提交于 2021-01-29 02:31:38
问题 Let's take a look with my enum definition here: public enum Day { MONDAY(1), TUESDAY(2), WEDNESDAY(3); int index; Day(int i) { index = i; } public void setIndex(int index) { this.index = index; } public static void main(String[] args) { Day x = MONDAY; Day y = MONDAY; x.setIndex(2); System.out.println(y.index); // Ouput: 2 } In general, I know we should not implement code like that. To prevent this, why java don't use final for final int index like Java treat with interface's properties. Does

Java NoSuchFieldError when using Reflection

怎甘沉沦 提交于 2021-01-28 06:41:50
问题 I'm trying to modify a public static final String[] field I made in ClassA, and then modify it in ClassB using reflection. However I get a NoSuchFieldException. java.lang.NoSuchFieldException: test at java.lang.Class.getField(Unknown Source) at packageA.ClassA.<init>(ClassA.java:17) ClassA is located in packageA and ClassB is located in packageB if that matters. Class A , creates the field and calls ClassB : package packageA; import packageB.ClassB; public class ClassA { // Create final

How to test class using content resolver/provider?

孤人 提交于 2021-01-20 16:43:21
问题 I'm trying to test class that queries content resolver. I would like to use MockContentResolver and mock query method. The problem is that this method is final. What should I do? Use mocking framework? Mock other class? Thanks in advance. public class CustomClass { private ContentResolver mContentResolver; public CustomClass(ContentResolver contentResolver) { mContentResolver = contentResolver; } public String getConfig(String key) throws NoSuchFieldException { String value = null; Cursor

Is it possible to extend a final class in Java?

做~自己de王妃 提交于 2020-12-12 04:02:13
问题 On possible duplicate: This thread is not asking how to extend a final class. It is asking why a class declared as final could possibly extend another class. From this thread: A final class is simply a class that can't be extended . However, I have a helper class which I declared to be final and extends another class: public final class PDFGenerator extends PdfPageEventHelper { private static Font font; private PDFGenerator() { // prevent instantiation } static { try { BaseFont baseFont =