final

In Java, can a final field be initialized from a constructor helper?

我是研究僧i 提交于 2019-12-05 08:23:29
问题 I have a final non-static member: private final HashMap<String,String> myMap; I would like to initialize it using a method called by the constructor. Since myMap is final, my "helper" method is unable to initialize it directly. Of course I have options: I could implement the myMap initialization code directly in the constructor. MyConstructor (String someThingNecessary) { myMap = new HashMap<String,String>(); myMap.put("blah","blahblah"); // etc... // other initialization stuff unrelated to

Variable assignment in lambda expression

穿精又带淫゛_ 提交于 2019-12-05 08:02:08
问题 I have the following fragment of code: SomeClass someClass; switch (type) { case FIRST: someClass = new SomeClass(); break; case SECOND: OptionalLong optional = findSomeOptional(); optional.ifPresent(value -> someClass = new SomeClass(value)); } And I'm trying to assign new object to someClass reference in lambda expresion but then I've got error message: "variable used in lambda should be effectively final" . When I add final to declaration of someClass I got another error "cannot assign

Why System class declared as final and with private constructor? [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-05 07:19:28
This question already has an answer here: Java — private constructor vs final and more 3 answers As per my understanding Final class A final class is simply a class that can't be extended. A class with single no argument private constructor A class with private constructors cannot be instantiated except form inside that same class. This make it useless to extend it from another class. But it does not mean it cannot be sub classed at all, among inner classes we can extend and call the private constructor. So my understanding is, if we create a class with single no argument private constructor,

【Java】final修饰符的使用

十年热恋 提交于 2019-12-05 05:12:21
final修饰符的使用   1.修饰类:     final修饰的类不能被继承,final修饰的类里面的方法都是(隐式)final方法   2.修饰方法:     final修饰的方法不能被重写   3.修饰变量(被修饰的变量一定要进行初始赋值):     1)基本类型变量:         final修饰的基本类型变量不能改变值     2)引用类型变量:         final修饰的引用类型变量的地址不能改变,即始终指向一个对象    来源: https://www.cnblogs.com/shuaiBqi/p/11907908.html

How transient works with final in Serialization Java

谁说胖子不能爱 提交于 2019-12-05 04:23:47
问题 I was reading about transient and final keyword and I found the answer that we can't use transient keyword with final keyword. I tried and got confused because here it working fine. import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.Serializable; public class SerExample{ public static void main(String... args){ Student foo = new Student(3,2,"ABC"); Student koo = new Student(6,4,"DEF"); try {

Reasoning for making a method final

↘锁芯ラ 提交于 2019-12-05 03:55:31
Sorry, quick question here, just found something in my notes I don't understand in relation to making a method final. My notes claim you should make a method final for this reason : Makes it impossible to enforce invariants. A String should behave as a String. I don't really understand what is meant by this. Can someone please break it down for me ? Thanks a lot. I would guess that should have said "Make it possible to enforce invariants". Basically, if someone can override a method, then they can change behaviors affecting your class's invariants. There are typically two reasons to make a

Might not have been initialized error at null check

可紊 提交于 2019-12-05 03:26:28
I'm checking if the variable is initialized but at that point netbeans is giving me variable reader might not have been initialized warning. How do I fix/suppress this? This is my code (summary): final Reader reader; try { reader = new Reader(directory); //additional stuff that can cause an exception } catch (Exception ex) { //do stuff } finally { if (reader != null); } The point of the if check is to determine whether it is initialized. And what is the best practice for this? If reader was never initialized, it doesn't even have a null value. change final Reader reader; to Reader reader =

Garbage collection when final variables used in anonymous classes

烈酒焚心 提交于 2019-12-05 03:14:15
If I have code similar to the following: public Constructor(final Object o) { taskSystem.add(new CycleTask(15, 15, -1) { @Override public void execute() throws Throwable { //access o here every 15 cycles forever } }); } When would o be garbage collected, if ever? Only when the task has been collected, or will it remain in memory forever because it's final? o might get garbage collected once it is not reachable any longer, whether it is final or not. Obviously, as long as execute is running, if it needs to access o , it will prevent GC. When execute is done running, and assuming you have not

What does the JVM do with a final variable in a class?

南楼画角 提交于 2019-12-05 01:20:45
问题 How does the JVM handle final variables differently under the hood? 回答1: There is at least one section in the JVM specification about final 's impact on the memory model, and it is quite important for multi-threaded code: final fields of an object allow "safe publication": When a constructor exits, all final fields must be visible to all threads. The fields on any object accessed via a final reference are also guaranteed to be at least as up to date as when the constructor exits. Taken

Final Fields Semantics in Threads

感情迁移 提交于 2019-12-04 23:28:33
问题 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