final

Java final static declarations in method local classes

陌路散爱 提交于 2019-11-30 11:38:37
When declaring a local inner class inside a method, why is it legal to include final static Strings or ints but not legal to include other objects? For instance: class Outer { void aMethod() { class Inner { final static String name = "compiles"; final static int ctr = 10; // compiles final static Integer intThree = Integer.valueOf(3); // does not compile! final static obj objConst = new Object(); // does not compile! } Inner inner = new Inner(); } } When I compile this, I get the following: InnerExample.java:6: inner classes cannot have static declarations final static Integer outer = Integer

java: how to declare final a variable that is initialized inside a try - catch block?

巧了我就是萌 提交于 2019-11-30 11:26:04
I have a variable that is not supposed to change its value after it's been initialized, so I want to define it as a final variable. the problem is that the variable has to be initialized inside a try block, so I get the following troubles: I have the following code: Connection conn = null; try { conn = getConn(prefix); [...do some stuff with conn...] } catch (Exception e) { throw new DbHelperException("error opening connection", e); } finally { closeConnection(conn); } If I declare the variabale as final, without initializing it to null, I get a 'The local variable conn may not have been

public static final variable in an imported java class

半城伤御伤魂 提交于 2019-11-30 11:11:51
I happen to come across a Java code at my work place. Here's the scenario: There are 2 classes - ClassA and ClassB . ClassA has nothing except 4 public static final string values inside it. Its purpose is to use those values like ClassA.variable (don't ask me why, it's not my code). ClassB imports ClassA . I edited the string values in ClassA and compiled it. When I ran ClassB I could see it was using the old values - not the new values. I had to recompile ClassB to make it use new values from ClassA ! (I had to recompile other classes that imports ClassA !) Is this just because of JDK 1.6 or

changing final variables through reflection, why difference between static and non-static final variable

独自空忆成欢 提交于 2019-11-30 09:08:56
Please refer to the below code. When I run the code, I am able to change the value of a final non-static variable. But if I try to change the value of a final static variable then it throws java.lang.IllegalAccessException . My question is why doesn't it throw an exception in case of non-static final variable also or vice versa. Why the difference? import java.lang.reflect.Field; import java.util.Random; public class FinalReflection { final static int stmark = computeRandom(); final int inmark = computeRandom(); public static void main(String[] args) throws SecurityException,

Cannot see final variable content inside anonymous class when debugging in Eclipse an Android app

≡放荡痞女 提交于 2019-11-30 08:47:05
When in debug (eclipse), I cannot see variables content in the variables view, nor in Expressions view, nor in Display view - if the variables are defined outside an anonymous class but the debug is inside the anonymous class. When I try to see the content in debug, I get the error: x cannot be resolved to a variable . In the following example, x cannot be resolved: private void someMethod(final Object x) { new Runnable() { public void run() { Log.i(x); // x is printed correctly but cannot be resolved when in Debug } }.run(); } This question regards the eclipse development environment -

final class in c++

我的梦境 提交于 2019-11-30 08:40:30
class Temp { private: ~Temp() {} friend class Final; }; class Final : virtual public Temp { public: void fun() { cout<<"In base"; } }; class Derived : public Final { }; void main() { Derived obj; obj.fun(); } The above code tries to achieve non-inheritable class (final). But using above code the object of derived can still be created, why? The desired functionality is achieved only if ctor made private, my question is why it is not achievable in case of dtor private? Well, for this program (pleasse provide correct, compilable examples) #include <iostream> class Temp { private: ~Temp() {}

Where are Java final local variables stored?

我是研究僧i 提交于 2019-11-30 06:45:41
Take the following example: public void init() { final Environment env = new Environment(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { env.close(); } }); } Firstly, where is env stored? Is it: copied by the compiler into a hidden member variable of the inner class that references it copied to, and referenced on, the heap left on the stack and somehow referenced there something else My guess is the first option. Secondly, do any performance issues that arise from doing this (rather than simply creating env as a member variable of the class and referencing it as such

Could a final variable be reassigned in catch, even if assignment is last operation in try?

怎甘沉沦 提交于 2019-11-30 05:50:24
问题 I am quite convinced that here final int i; try { i = calculateIndex(); } catch (Exception e) { i = 1; } i cannot possibly have already been assigned if control reaches the catch-block. However, Java compiler disagrees and claims the final local variable i may already have been assigned . Is there still some subtlety I am missing here, or is this just a weakness of the model used by the Java Language Specification to identify potential reassignments? My main worry are things like Thread.stop(

How does the compiler benefit from C++'s new final keyword?

大兔子大兔子 提交于 2019-11-30 05:48:12
C++11 will allow to mark classes and virtual method to be final to prohibit deriving from them or overriding them. class Driver { virtual void print() const; }; class KeyboardDriver : public Driver { void print(int) const final; }; class MouseDriver final : public Driver { void print(int) const; }; class Data final { int values_; }; This is very useful, because it tells the reader of the interface something about the intent of the use of this class/method. That the user gets diagnostics if he tries to override might be useful, too. But is there an advantage from the compilers point of view?

Is “public static final” redundant for a constant in a Java interface?

坚强是说给别人听的谎言 提交于 2019-11-30 05:36:25
This code: interface Config { int MAX_CONN = 20; } compiled and worked as I expected. It looks like this is the same as: interface Config { public static final int MAX_CONN = 20; } Is "public static final" redundant for a constant in a Java interface? Is this true for Java 1.1, 1.2, 1.3, 1.4,..., 1.8 or did it change in a Java release? NINCOMPOOP Variables declared in Interface are implicitly public static final . This is what JLS 9.3 says : Every field declaration in the body of an interface is implicitly public, static, and final . It is permitted to redundantly specify any or all of these