final

Creating an immutable object without final fields?

送分小仙女□ 提交于 2019-12-04 12:06:16
问题 Can we create an immutable object without having all fields final? If possible a couple of examples would be helpful. 回答1: Declare all fields private and only define getters: public final class Private{ private int a; private int b; public int getA(){return this.a;} public int getB(){return this.b;} } citing @Jon Skeet's comment, final class modifier is useful for: While an instance of just Private is immutable, an instance of a subclass may well be mutable. So code receiving a reference of

Why String is immutable or final in Java [duplicate]

空扰寡人 提交于 2019-12-04 11:32:07
问题 This question already has answers here : Why is the String class declared final in Java? (16 answers) Closed 6 years ago . As i was told this is important String Interview question in Java, which starts with discussion of " What is String ", how String is different in java than in C or C++ and then you are asked about immutable objects and you're asked the main question: " Why String is immutable or final in Java ". Can you share your Ideas ? Thanks in advance. 回答1: It is mainly for security

Should every immutable class be final?

老子叫甜甜 提交于 2019-12-04 11:12:51
I was designing a Card class to be used in a Blackjack game. My design was to make a Card class with a getValue() that returns, for example, 11 for J, 12 for Q and 13 for K, and then extend it with a BlackjackCard class to override that method so that those cards return 10. Then something hit me: objects of the Card class should be immutable. So I re-read Effective Java 2nd Edition to see what to do and I there I found that immutable classes need to be final, to avoid a subclass to break the immutability. I also looked in Internet and everyone seems to agree in that point. So should the Card

How to prevent the changing of a variable value in Javascript [duplicate]

拥有回忆 提交于 2019-12-04 10:31:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Are there constants in Javascript? Is there a way to declare a static of final value in javascript so that it cannot be altered by a 3rd party? What I have is a article sharing application, with free users being ad supported. What I wish to do is prevent the free users from altering the innerHTML content by altering the storage variable for the content. What I have at this moment is a timer that reloads the

Why can an anonymous class access non-final class member of the enclosing class

倖福魔咒の 提交于 2019-12-04 09:12:47
问题 We know that only final local variables can be accessed in an anonymous class, and there is a good reason here: Why are only final variables accessible in anonymous class?. However, I found that an anonymous class can still access a non-final variable if the variable is an member field of the enclosing class: How can I access enclosing class instance variables from inside the anonymous class? I am confused. We ensure that only a final local variable can be accessed in anonymous class because

Java method keyword “final” and its use

安稳与你 提交于 2019-12-04 08:18:32
When I create complex type hierarchies (several levels, several types per level), I like to use the final keyword on methods implementing some interface declaration. An example: interface Garble { int zork(); } interface Gnarf extends Garble { /** * This is the same as calling {@link #zblah(0)} */ int zblah(); int zblah(int defaultZblah); } And then abstract class AbstractGarble implements Garble { @Override public final int zork() { ... } } abstract class AbstractGnarf extends AbstractGarble implements Gnarf { // Here I absolutely want to fix the default behaviour of zblah // No Gnarf shouldn

Strange Java behaviour with static and final qualifiers [duplicate]

瘦欲@ 提交于 2019-12-04 07:35:15
问题 This question already has answers here : Final fields initialization order (2 answers) Closed 3 years ago . In our team we found some strange behaviour where we used both static and final qualifiers. This is our test class: public class Test { public static final Test me = new Test(); public static final Integer I = 4; public static final String S = "abc"; public Test() { System.out.println(I); System.out.println(S); } public static Test getInstance() { return me; } public static void main

Java Concurrency : Volatile vs final in “cascaded” variables?

て烟熏妆下的殇ゞ 提交于 2019-12-04 07:31:49
is final Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); the same as volatile Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); in case the inner Map is accessed by different Threads? or is even something like this required: volatile Map

java:final关键字

一世执手 提交于 2019-12-04 06:03:31
final关键字的四周用法:   1:用来修饰类     被final修饰的类不能被继承,里面的方法自然不能被覆盖重写   2:用来修饰方法     被final修饰的方法不能被覆盖重写,不能和abstract关键字同时使用   3:用来修饰成员变量     被final修饰的成员变量也不变,必须要手动赋值,要么直接赋值,要么通过构造方法间接赋值。   4:用来修饰局部变量     被final关键字修饰的局部变量,一次赋值,终生不变。 来源: https://www.cnblogs.com/BatmanY/p/11834640.html

composite inheritance: how to assign a final field at sub-class constructor which depends on 'this' value (backward reference)?

时间秒杀一切 提交于 2019-12-04 05:31:47
问题 I use composite classes to group functionalities. But, the class A (with composite A1), got inherited by B (with composite B1), and a behavior existent at A1 is going to be adapted at B1, but the final a1 must be a B1 instance for this to work. Obs.: I have ways to make sure the composite instantiation happens properly (only by its composite partner). Unable to assign a B1 object to a1 final field: class finalFieldTestFails{ class A1{ A1(A a){} } class A{ protected final A1 a1; A(){ this.a1 =