final

final transient fields and serialization

混江龙づ霸主 提交于 2019-11-27 10:25:56
问题 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

What is the purpose of the “final” keyword in C++11 for functions?

雨燕双飞 提交于 2019-11-27 10:24:37
What is the purpose of the final keyword in C++11 for functions? I understand it prevents function overriding by derived classes, but if this is the case, then isn't it enough to declare as non-virtual your final functions? Is there another thing I'm missing here? What you are missing, as idljarn already mentioned in a comment is that if you are overriding a function from a base class, then you cannot possibly mark it as non-virtual: struct base { virtual void f(); }; struct derived : base { void f() final; // virtual as it overrides base::f }; struct mostderived : derived { //void f(); //

Modifying final fields in Java

自闭症网瘾萝莉.ら 提交于 2019-11-27 09:57:03
问题 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

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

自古美人都是妖i 提交于 2019-11-27 09:51:00
问题 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

Java static final values replaced in code when compiling?

…衆ロ難τιáo~ 提交于 2019-11-27 08:24:18
In java, say I have the following ==fileA.java== class A { public static final int SIZE = 100; } Then in another file i use this value ==fileB.java== import A; class b { Object[] temp = new Object[A.SIZE]; } When this gets compiled does SIZE get replaced with the value 100, so that if i were to down the road replace the FileA.jar but not FileB.jar would the object array get the new value or would it have been hardcoded to 100 because thats the value when it was originally built? Thanks, Stephanie Yes, the Java compiler does replace static constant values like SIZE in your example with their

What's the point of a final virtual function?

巧了我就是萌 提交于 2019-11-27 07:56:48
Wikipedia has the following example on the C++11 final modifier: struct Base2 { virtual void f() final; }; struct Derived2 : Base2 { void f(); // ill-formed because the virtual function Base2::f has been marked final }; I don't understand the point of introducing a virtual function and immediately marking it as final. Is this simply a bad example, or is there more to it? Typically final will not be used on the base class' definition of a virtual function. final will be used by a derived class that overrides the function in order to prevent further derived types from further overriding the

creating final variables inside a loop

喜你入骨 提交于 2019-11-27 07:45:16
is this allowed in java: for(int i=0;i<5;i++){ final int myFinalVariable = i; } The keyword of my question is final . Is it allowed to do a final variable that changes with every run of the loop? I was wondering this because final says that you can't change the value of the variable (calling only myFinalVariable = i ), but i'm redefining the whole variable with final int . Are they two completely different variables just with the same name - with the variable from the previous run of the loop already heading down the road to the garbage collector? Yes, it is allowed. The final keyword means

Testing initialization safety of final fields

扶醉桌前 提交于 2019-11-27 07:06:22
I am trying to simply test out the initialization safety of final fields as guaranteed by the JLS. It is for a paper I'm writing. However, I am unable to get it to 'fail' based on my current code. Can someone tell me what I'm doing wrong, or if this is just something I have to run over and over again and then see a failure with some unlucky timing? Here is my code: public class TestClass { final int x; int y; static TestClass f; public TestClass() { x = 3; y = 4; } static void writer() { TestClass.f = new TestClass(); } static void reader() { if (TestClass.f != null) { int i = TestClass.f.x; /

How to extend a final class in Java

巧了我就是萌 提交于 2019-11-27 06:37:33
问题 Here is my problem actually I'm facing now. I have a class, let's say Foo and this class defines a method called getBar that returns a Bar instance. The class Bar is defined inside Foo and and is declared public static final . What I want to do is to define a class MyFoo that extends Foo but I also want to extend Bar with MyBar by adding my own functionalities (methods, properties,etc). I also want getBar to return MyBar . The problem is Bar is final. Here is an illustration of what I want to

Making java method arguments as final

最后都变了- 提交于 2019-11-27 06:24:15
What difference that final makes between the code below. Is there any advantage in declaring the arguments as final . public String changeTimezone( Timestamp stamp, Timezone fTz, Timezone toTz){ return .... } public String changeTimezone(final Timestamp stamp, final Timezone fTz, final Timezone toTz){ return .... } PeterMmm As a formal method parameter is a local variable, you can access them from inner anonymous classes only if they are declared as final. This saves you from declaring another local final variable in the method body: void m(final int param) { new Thread(new Runnable() { public