final

final关键字

雨燕双飞 提交于 2019-11-28 21:45:00
/* * final:最终的 * * 1. final可以用来修饰的结构:类、方法、变量 * * 2. final 用来修饰一个类:此类不能被其他类所继承。 * 比如:String类、System类、StringBuffer类 * * 3. final 用来修饰方法:此方法不可以被重写。 * 比如:Object类中getClass(); * * 4. final 用来修饰变量:此时的"变量"就称为是一个常量 * 4.1 final修饰属性:可以考虑赋值的位置有:显式初始化、代码块中初始化、构造器中初始化 * 4.2 final修饰局部变量: * 尤其是使用final修饰形参时,表明此形参是一个常量。当我们调用此方法时,给常量形参赋一个实参 , * 一旦赋值以后,就只能在方法体内使用此形参,但不能进行重新赋值。 * * static final用来修饰属性:全局常量 */ 来源: https://blog.csdn.net/qq_38022403/article/details/100127403

String and Final

巧了我就是萌 提交于 2019-11-28 21:10:40
What is difference between in the following statements String name = "Tiger"; final String name ="Tiger"; Although the String class is final class, why do we need to create a String "CONSTANT" variable as final? final in this context means that the variable name can only be assigned once. Assigning a different String object to it again results in a compile error. I think the source of the confusion here is that the final keyword can be used in several different contexts: final class: The class cannot be subclassed. final method: The method cannot be overridden. final variable: The variable can

Why attempt to print uninitialized variable does not always result in an error message

試著忘記壹切 提交于 2019-11-28 20:45:45
问题 Some may find it similar to the SO question Will Java Final variables have default values? but that answer doesn't completely solve this, as that question doesn't directly print the value of x within instance initializer block. The problem arises when I try to print x directly inside the instance initializer block, while having assigned a value to x before the end of the block : Case 1 class HelloWorld { final int x; { System.out.println(x); x = 7; System.out.println(x); } HelloWorld() {

final variable in methods in Java [duplicate]

↘锁芯ラ 提交于 2019-11-28 20:14:16
Possible Duplicate: Why would one mark local variables and method parameters as “final” in Java? I was checking some Java code, I am not good at java at least have some knowledge what final does such as sealed classes, readonly fields and non-overridable methods but this looks weird to me, declaring a variable final in methods: private static void queryGoogleBooks(JsonFactory jsonFactory, String query) throws Exception { // Set up Books client. final Books books = Books.builder(new NetHttpTransport(), jsonFactory) .setApplicationName("Google-BooksSample/1.0") .setJsonHttpRequestInitializer(new

About reference to object before object's constructor is finished

核能气质少年 提交于 2019-11-28 18:54:38
Every one of you know about this feature of JMM , that sometimes reference to object could receive value before constructor of this object is finished. In JLS7, p. 17.5 final Field Semantics we can also read: The usage model for final fields is a simple one: Set the final fields for an object in that object's constructor; and 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

Quick Java question about private static final keywords for fields

℡╲_俬逩灬. 提交于 2019-11-28 18:37:50
I'm declaring a field: private static final String filename = "filename.txt"; First, does the order of private static final matter? If not, is there a standard accepted sequence or convention? Second, the filename in my application is fixed. Is this the best was to store its value? I use Checkstyle with Eclipse, which results in a warning if the declaration is in a different order to the one you've specified, citing the Java Language Specification (JLS). For example, private final static String filename = "filename.txt"; results in 'static' modifier out of order with the JLS suggestions. They

Are final static variables thread safe in Java?

烂漫一生 提交于 2019-11-28 17:53:06
I've read around quite a bit but haven't found a definitive answer. I have a class that looks like this: public class Foo() { private static final HashMap<String, HashMap> sharedData; private final HashMap myRefOfInnerHashMap; static { // time-consuming initialization of sharedData final HashMap<String, String> innerMap = new HashMap<String, String>; innerMap.put... innerMap.put... ...a sharedData.put(someKey, java.util.Collections.unmodifiableMap(innerMap)); } public Foo(String key) { this.myRefOfInnerHashMap = sharedData.get(key); } public void doSomethingUseful() { // iterate over copy for

final transient fields and serialization

梦想的初衷 提交于 2019-11-28 17:43:35
Is it possible to have final transient fields that are set to any non-default value after serialization in Java? My usecase is a cache variable — that's why it is transient . I also have a habit of making Map fields that won't be changed (i.e. contents of the map is changed, but object itself remains the same) final . However, these attributes seem to be contradictory — while compiler allows such a combination, I cannot have the field set to anything but null after unserialization. I tried the following, without success: simple field initialization (shown in the example): this is what I

Modifying final fields in Java

牧云@^-^@ 提交于 2019-11-28 16:57:48
Let's start with a simple test case: import java.lang.reflect.Field; public class Test { private final int primitiveInt = 42; private final Integer wrappedInt = 42; private final String stringValue = "42"; public int getPrimitiveInt() { return this.primitiveInt; } public int getWrappedInt() { return this.wrappedInt; } public String getStringValue() { return this.stringValue; } public void changeField(String name, Object value) throws IllegalAccessException, NoSuchFieldException { Field field = Test.class.getDeclaredField(name); field.setAccessible(true); field.set(this, value); System.out

Is it a bad idea to declare a final static method?

元气小坏坏 提交于 2019-11-28 16:41:12
I understand that in this code: class Foo { public static void method() { System.out.println("in Foo"); } } class Bar extends Foo { public static void method() { System.out.println("in Bar"); } } .. the static method in Bar 'hides' the static method declared in Foo , as opposed to overriding it in the polymorphism sense. class Test { public static void main(String[] args) { Foo.method(); Bar.method(); } } ...will output: in Foo in Bar Re-defining method() as final in Foo will disable the ability for Bar to hide it, and re-running main() will output: in Foo in Foo ( Edit : Compilation fails