final

Java: Meaning of catch (final SomeException e)?

我们两清 提交于 2019-11-28 06:16:18
What does final do in the following Java expression? catch (final SomeExceptionType e) It basically means: Catch "SomeExceptionType" into the variable "e" with the promise that we won't assign a different exception to "e" during the processing of the exception. Mostly this is overkill, as if I'm catching an exception into a temporary variable name (e only is valid for the exception handling block), I don't have to police myself so strictly as to not trust myself to assign a different (possibly created) exception to the same variable name. That said, perhaps this block is heavily maintained by

Make private methods final?

痞子三分冷 提交于 2019-11-28 06:09:57
Is it beneficial to make private methods final? Would that improve performance? I think "private final" doesn't make much sense, because a private method cannot be overridden. So the method lookup should be efficient as when using final. And would it be better to make a private helper method static (when possible)? What's best to use? private Result doSomething() private final Result doSomething() private static Result doSomething() private static final Result doSomething() Adding final to methods does not improve performance with Sun HotSpot. Where final could be added, HotSpot will notice

Java final

怎甘沉沦 提交于 2019-11-28 05:02:54
/* * final:是一个关键字,最后的,最终的.被final修饰的内容是不能再被改变的. * * 可以修饰的内容: * 1.类:final修饰的类不能有子类 * 2.成员变量:变量是一个终值,不能再被改变.所以在定义时必须先手动给一个值. * 3.局部变量:被final修饰的局部变量是一个终值,不能再被改变 * 4.方法:final修饰的方法不允许重写 * 5.空白final----了解 */ public class Demo1 { public static void main(String[] args) { Ainmal ainmal = new Ainmal(); } } class Ainmal{ //这里的红色不是初始化,是赋值 //当构造方法执行的时候,会立刻对属性进行初始化,引用类型赋值成null.接下来才会去读取红色这个值并赋值给color String color = "红色"; final String name = "冰冰" ;//2.成员变量:变量是一个终值,不能再被改变.所以在定义时必须先手动给一个值. public Ainmal() { //对自己的属性进行初始化 super();//调用父类的构造方法 //做自己的事情 System.out.println("自己的功能"); //因为name已经是一个final的,不能再被改变. //name

Why final keyword is necessary for immutable class?

▼魔方 西西 提交于 2019-11-28 04:34:05
Could you please clarify that why final keyword is required before class when we are making it an immutable one. I mean, if we declare all of it's attributes as private and final, then also it is an immutable class, isn't it? Sorry if the question seems easy, but i am truly confused about it. Help me out. Editted: I know that a class declared final can't be subclassed.. But if each attribute is private and final then what difference does that make? Jon Skeet As stacker says, final makes sure the class isn't subclassed. That's important so that any code which is relying on its immutability can

Java final field compile-time constant expression

♀尐吖头ヾ 提交于 2019-11-28 04:13:47
问题 The below text is from jls http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.3 Even then, there are a number of complications. If a final field is initialized to a compile-time constant expression (§15.28) in the field declaration, changes to the final field may not be observed, since uses of that final field are replaced at compile time with the value of the constant expression. Can anyone Please give me better explanation for the above. i couldn't understand the

Will Java Final variables have default values?

一世执手 提交于 2019-11-28 03:31:47
I have a program like this: class Test { final int x; { printX(); } Test() { System.out.println("const called"); } void printX() { System.out.println("Here x is " + x); } public static void main(String[] args) { Test t = new Test(); } } If I try to execute it, i am getting compiler error as : variable x might not have been initialized based on java default values i should get the below output right?? "Here x is 0". Will final variables have dafault values? if I change my code like this, class Test { final int x; { printX(); x = 7; printX(); } Test() { System.out.println("const called"); } void

Equivalent of const(C++) in Java

半腔热情 提交于 2019-11-28 02:30:42
问题 I was wondering if there was an equivalent to c++'s const in Java. I understand the final keyword, but unfortunately I cannot use that to declare a functions return value final. Instead, it always ensures the function cannot be overridden, correct? Basically, I want to make sure a given returned class cannot be modified and is read only. Is that possible in Java? 回答1: Basically, I want to make sure a given returned class cannot be modified and is read only. Is that possible in Java? Not

How is concatenation of final strings done in Java?

守給你的承諾、 提交于 2019-11-27 23:33:16
When I compile this snippet. public class InternTest { public static void main(String...strings ){ final String str1="str"; final String str2="ing"; String str= str1+str2; } } Which produces the following byte code public static void main(java.lang.String...); flags: ACC_PUBLIC, ACC_STATIC, ACC_VARARGS Code: stack=1, locals=4, args_size=1 0: ldc #16 // String str 2: astore_1 3: ldc #18 // String ing 5: astore_2 6: ldc #20 // String string 8: astore_3 9: return so string literal "string" is already there in the constant pool which gets pushed 6: ldc #20 // String string on stack at this line.

How does java serialization deserialize final fields when no default constructor specified?

淺唱寂寞╮ 提交于 2019-11-27 21:46:40
I have an class defining an immutable value type that I now need to serialize. The immutability comes from the final fields which are set in the constructor. I've tried serializing, and it works (surprisingly?) - but I've no idea how. Here's an example of the class public class MyValueType implements Serializable { private final int value; private transient int derivedValue; public MyValueType(int value) { this.value = value; this.derivedValue = derivedValue(value); } // getters etc... } Given that the class doesn't have a no arg constructor, how can it be instantiated and the final field set?

Git merge testing branch (final commit) to master branch

邮差的信 提交于 2019-11-27 21:26:43
问题 I have created a testing branch. It has a lot of tiny commits to build one feature. At the end of it, I want to take the final completed changes, and put them into the master branch. The master branch, shouldn't contain the history of the testing branch. Testing branch will be removed eventually. What is the best way to achieve this? Would generating a patch and applying it on master be the best way? If so, how do I generate/apply the patch? 回答1: Various approaches are described in