final

Java - final variables

可紊 提交于 2019-11-27 16:17:08
问题 I know that once a final variable has a value assigned to it, it cannot be changed. However I just have a couple of questions regarding this: When I have a field, say static final JButton button; outside a class, and then in the main method, try to assign it a value, button = new JButton("OK"); , I get an error telling me to remove the final modifier? However since the original button variable does not yet reference an object I was under the impression I could assign it once? Secondly, if I

Is there any way to prevent replacement of JavaScript object properties?

安稳与你 提交于 2019-11-27 16:12:50
问题 I would like to make an object's structure immutable, preventing its properties from being subsequently replaced. The properties need to be readable, however. Is this possible? I'm sure there are no language features (along the lines of final in Java and readonly in C#) to support this but wondered whether there might be another mechanism for achieving the same result? I'm looking for something along these lines: var o = { a: "a", f: function () { return "b"; } }; var p = o.a; // OK o.a = "b"

In Java, why can't I declare a final member (w/o initializing it) in the parent class and set its value in the subclass? How can I work around?

百般思念 提交于 2019-11-27 16:03:49
In a Java program, I have multiple subclasses inheriting from a parent (which is abstract). I wanted to express that every child should have a member that is set once only (which I was planning to do from the constructor). My plan was to code s.th. like this: public abstract class Parent { protected final String birthmark; } public class Child extends Parent { public Child(String s) { this.birthmark = s; } } However, this seems to not please the Java gods. In the parent class, I get the message that birthmark "might not have been initialized", in the child class I get "The final field

Should I declare a java field 'final' when if it's not modified in code?

大兔子大兔子 提交于 2019-11-27 15:58:19
问题 My question is mainly about performance. The compiler knows better that, for example, some variable is NOT modified after object instantiation. So, why bother with final? I presume many structural/logical reasons might come here, but speaking from the performance point of view? Does it matter? Thanks, 回答1: In a modern JVM, final shouldn't affect performance. This is especially true for private fields, but even for non-private fields the JIT can optimize non-final fields as thought they are

Why Final variable doesn't require initialization in main method in java?

那年仲夏 提交于 2019-11-27 15:31:09
When I am just trying to do some program in Java .I try to use final variable,I know that final variable must be initialized at the time of declaration, but inside the main method it accepts the final variable with out initialization. I don't know what's the reason.Can any one tell me the reason. Thank you code: class name { final int b; //here shows error public static void main(String args[]) { final int a; // here no error... why? System.out.println("hai"); } } For instance variable level A final variable can be initialized only once. A final variable at class level must be initialized

Scala - initialization order of vals

允我心安 提交于 2019-11-27 15:09:19
I have this piece of code that loads Properties from a file: class Config { val properties: Properties = { val p = new Properties() p.load(Thread.currentThread().getContextClassLoader.getResourceAsStream("props")) p } val forumId = properties.get("forum_id") } This seems to be working fine. I have tried moving the initialization of properties into another val, loadedProperties , like this: class Config { val properties: Properties = loadedProps val forumId = properties.get("forum_id") private val loadedProps = { val p = new Properties() p.load(Thread.currentThread().getContextClassLoader

Why do variables passed to runnable need to be final?

℡╲_俬逩灬. 提交于 2019-11-27 15:07:12
If I have a variable int x = 1 , say, and I declare a runnable in the main thread, and I want to pass x to the runnable's run() method, it must be declared final . Why? final int x = 0;//<----must be final... private class myRun implements Runnable { @Override public void run() { x++;// } } Because if they are able to be changed, it could cause a lot of problems, consider this: public void count() { int x; new Thread(new Runnable() { public void run() { while(x < 100) { x++; try { Thread.sleep(1000); }catch(Exception e){} } } }).start(); // do some more code... for(x = 0;x < 5;x++) for(int y =

override java final methods via reflection or other means?

。_饼干妹妹 提交于 2019-11-27 15:02:00
问题 This question arise while trying to write test cases. Foo is a class within the framework library which I dont have source access to. public class Foo{ public final Object getX(){ ... } } my applications will public class Bar extends Foo{ public int process(){ Object value = getX(); ... } } The unit test case is unable to initalize as I can't create a Foo object due to other dependencies. The BarTest throws a null pointer as value is null. public class BarTest extends TestCase{ public

Anonymous-Inner classes showing unwanted modifier

倾然丶 夕夏残阳落幕 提交于 2019-11-27 14:55:59
To my understanding, the following code should have printed true . However, when I ran this code it is printing false . From Java docs of Anonymous Classes 15.9.5. : An anonymous class is always implicitly final public class Test { public static void main(String args[]) { Object o = new Object() { }; System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers())); } } Can some one please help me understand this behavior. Note that the wording in the JLS of that particular section has changed significantly since then. It now (JLS 11) reads: 15.9.5. Anonymous

Java static final field initialization order

我是研究僧i 提交于 2019-11-27 14:54:32
I tried to understand the behavior of initialization order when static fields are initialized with a reference to the same enclosing class object. public class Test { static final Test t=new Test(); static int a=5; Test(){ System.out.println("a="+a); } public static void main(String[] args) { new Test(); } } Output of above piece of code is: a=0 a=5 If I modify variable a to anything else other than plain static : static final a=5; a=5; final a=5; The output is: a=5 a=5 Why is this behavior? Note that the output is a=5 & a=5 even when both t & a are declared as static final in which case t