final

final variable case in switch statement

让人想犯罪 __ 提交于 2019-11-29 03:12:32
final int a = 1; final int b; b = 2; final int x = 0; switch (x) { case a:break; // ok case b:break; // compiler error: Constant expression required } /* COMPILER RESULT: constant expression required case b:break; ^ 1 error */ Why am I getting this sort of error? If I would have done final int b = 2 , everything works. b may not have been initialized and it is possible to be assigned multiple values. In your example it is obviously initialized, but probably the compiler doesn't get to know that (and it can't). Imagine: final int b; if (something) { b = 1; } else { b = 2; } The compiler needs a

Passing final variables to anonymous classes

有些话、适合烂在心里 提交于 2019-11-29 02:11:52
问题 In final variable passed to anonymous class via constructor, Jon Skeet mentioned that variables are passed to the anonymous class instance via an auto-generated constructor. Why would I not be able to see the constructor using reflection in that case: public static void main(String... args) throws InterruptedException { final int x = 100; new Thread() { public void run() { System.out.println(x); for (Constructor<?> cons : this.getClass() .getDeclaredConstructors()) { StringBuilder str = new

Why private method can not be final as well?

拥有回忆 提交于 2019-11-29 02:06:35
问题 Is it redundant to add private and final to a same method? class SomeClass { //--snip-- private final void doStuff() { // private work here } } If it's private , there's no way anyone can override it, right? Why is it possible to add final keyword if it has no effect? (or am I missing something?) 回答1: Basically, it's allowed because they didn't feel like it's worthwhile to put a special case prohibiting the private modifier. It's like how you can also declare methods on an interface as public

Java - final variables

妖精的绣舞 提交于 2019-11-29 01:54:44
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 completely remove reference to the button so I just have static final JButton button; outside the class,

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

狂风中的少年 提交于 2019-11-29 01:45:40
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"; // Error var q = o.f(); // OK o.f = function () { // Error return "c"; }; ECMAScript 5 will have seal

why are java constants declared static?

陌路散爱 提交于 2019-11-29 01:03:06
Why are java constants declared static ? class Foo{ static final int FII = 2 ; } In this I understand the use of final? Buy why does it have to be static? Why should it be a class variable, and not an instance variable? If it could vary by the instance of a class, then it's clearly not a constant . What would it mean to get a different value of pi for each instance of Math (not that Math even allows instances to be constructed)? Or a different case insensitive ordering for each instance of String ? If a constant is not static, Java will allocate a memory for that constant in every object of

Git merge testing branch (final commit) to master branch

那年仲夏 提交于 2019-11-29 00:29:54
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? Various approaches are described in " Understanding the Git Workflow ": Short lived work The vast majority of the time, my cleanup is just a squash merge.

Why can't a Java enum be final?

人走茶凉 提交于 2019-11-28 23:03:22
public interface Proposal { public static final enum STATUS { NEW , START , CONTINUE , SENTTOCLIENT }; } Java does not allow an enum to be final inside an interface, but by default every data member inside an interface is public static final . Can anybody clarify this? Eugene Kuleshov An enum can't be final, because the compiler will generate subclasses for each enum entry that the programmer has explicitly defined an implementation for. Moreover, an enum where no instances have their own class body is implicitly final, by JLS section 8.9 . Java does not allow you to create a class that

Use of final local variables in java [duplicate]

陌路散爱 提交于 2019-11-28 22:36:50
This question already has an answer here: Why would one mark local variables and method parameters as “final” in Java? [closed] 12 answers I was wondering is there any usability of using final local variables. Variables are not overridden anyway when inheritance comes into picture. For example a simple code as below public static void main(String args[]) { final String data = "Hello World!"; System.out.println(data); } The example is quite simple one and may not be a relevant code but the question is more generic.I have seen a lot of codes(all incorporated in main function which have final

How to create a variable that can be set only once but isn't final in Java

橙三吉。 提交于 2019-11-28 22:27:44
I want a class that I can create instances of with one variable unset (the id ), then initialise this variable later, and have it immutable after initialisation . Effectively, I'd like a final variable that I can initialise outside of the constructor. Currently, I'm improvising this with a setter that throws an Exception as follows: public class Example { private long id = 0; // Constructors and other variables and methods deleted for clarity public long getId() { return id; } public void setId(long id) throws Exception { if ( this.id == 0 ) { this.id = id; } else { throw new Exception("Can't