final

Does using final for variables in Java improve garbage collection?

梦想的初衷 提交于 2019-11-27 06:12:17
Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection. For example, if you write a method like: public Double doCalc(final Double value) { final Double maxWeight = 1000.0; final Double totalWeight = maxWeight * value; return totalWeight; } Declaring the variables in the method final would help the garbage collection to clean up the memory from the unused variables in the method after the method exits. Is this true? Here's a slightly different example, one with final reference-type fields rather than final value-type local

Why final keyword is necessary for immutable class?

久未见 提交于 2019-11-27 05:22:41
问题 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? 回答1: As stacker says, final makes sure the

Final variable assignment with try/catch

折月煮酒 提交于 2019-11-27 05:13:46
Because I believe it is a good programming practice, I make all my (local or instance) variables final if they are intended to be written only once. However, I notice that when a variable assignment can throw an exception you cannot make said variable final: final int x; try { x = Integer.parseInt("someinput"); } catch(NumberFormatException e) { x = 42; // Compiler error: The final local variable x may already have been assigned } Is there a way to do this without resorting to a temporary variable? (or is this not the right place for a final modifier?) One way to do this is by introducing a

Will Java Final variables have default values?

霸气de小男生 提交于 2019-11-27 05:09:36
问题 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

when exactly are we supposed to use “public static final String”?

不问归期 提交于 2019-11-27 05:07:15
问题 I have seen much code where people write public static final String mystring = ... and then just use a value. Why do they have to do that? Why do they have to initialize the value as final prior to using it? UPDATE Ok, thanks all for all your answers, I understand the meaning of those key (public static final). What I dont understand is why people use that even if the constant will be used only in one place and only in the same class. why declaring it? why dont we just use the variable? 回答1:

Java : in what order are static final fields initialized?

断了今生、忘了曾经 提交于 2019-11-27 04:45:07
Okay, so say I have a class that looks like this : public class SignupServlet extends HttpServlet { private static final Logger SERVLET_LOGGER=COMPANYLog.open(SignupServlet.class); private static final ExceptionMessageHandler handler = new ExceptionMessageHandler(); private static final SignupServletObservableAgent signupObservableAgent = new SignupServletObservableAgent(null, SERVLET_LOGGER); } Can I count on the class loader to initialize those fields in order, such that I can rely on SERVLET_LOGGER to be instantiated before signupObservableAgent? Laurence Gonsalves Yes, they are initialized

Can a static method be overridden in C#?

女生的网名这么多〃 提交于 2019-11-27 04:28:42
I was told that static methods are implicitly final and therefore can't be overridden. Is that true? Can someone give a better example of overriding a static method? If static methods are just class methods, what is the real use of having them? (1) Static methods cannot be overridden, they can however be hidden using the 'new' keyword. Mostly overriding methods means you reference a base type and want to call a derived method. Since static's are part of the type and aren't subject to vtable lookups that doesn't make sense. E.g. statics cannot do: public class Foo { public virtual void Bar() {

Java static final field initialization order

情到浓时终转凉″ 提交于 2019-11-27 04:06:09
问题 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?

The final local variable cannot be assigned

﹥>﹥吖頭↗ 提交于 2019-11-27 03:20:43
I have an array of seats, and the array has two strings(selected and empty). On mouse click, I want to traverse the array and find the selected seat. When I press the button it says: The final local variable seatno cannot be assigned, since it is defined in an enclosing type. JButton btnContinue = new JButton("Next"); btnContinue.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { for(int x=0;x<17;x++){ if(anArray[x]=="selected"){ seatno = anArray[x]; } } data page=new data(newfrom,newto,newtime,date2,seatno); page.setVisible(true); setVisible(false); } });

Why is a `val` inside an `object` not automatically final?

前提是你 提交于 2019-11-27 03:03:12
问题 What is the reason for val s not (?) being automatically final in singleton objects? E.g. object NonFinal { val a = 0 val b = 1 def test(i: Int) = (i: @annotation.switch) match { case `a` => true case `b` => false } } results in: <console>:12: error: could not emit switch for @switch annotated match def test(i: Int) = (i: @annotation.switch) match { ^ Whereas object Final { final val a = 0 final val b = 1 def test(i: Int) = (i: @annotation.switch) match { case `a` => true case `b` => false }