final

Why are we allowed to have a final main method in java?

浪尽此生 提交于 2019-12-04 04:13:30
Can anyone tell me the use of making main method as final in java. while this is allowed in java public static final void main(String[] args) { } I dont see any use of making it final. anyways it is static so we can not override it. aioobe Adding final to a static method can actually make a difference. Consider the following code: class A { public static void main(String[] args) { System.out.println("A"); } } class B extends A { public static void main(String[] args) { System.out.println("B"); } } class C extends B { } public class Test { public static void main(String[] args) { C.main(args);

Why is Java's BigDecimal class not declared as final?

淺唱寂寞╮ 提交于 2019-12-04 03:49:37
While checking the source code of Java's BigDecimal class, I was surprised that it was not declared as a final class : Class BigDecimal public class BigDecimal extends Number implements Comparable<BigDecimal> Immutable , arbitrary-precision signed decimal numbers. (from the Oracle Docs ) Is there a specific reason for this or did the developers just forget to add that keyword? Is it a good practice to not declare immutable classes as final? The same goes for BigInteger , but not for String which is declared as final. Quote from https://blogs.oracle.com/darcy/entry/compatibly_evolving

Can excessive use of final hurt more than do good?

你说的曾经没有我的故事 提交于 2019-12-04 02:55:57
Why are people so emphatic about making every variable within a class "final"? I don't believe that there is any true benefit to adding final to private local variables, or really to use final for anything other than constants and passing variables into anonymous inner classes. I'm not looking to start any sort of flame war, I just honestly want to know why this is so important to some people. Am I missing something? Intent. Other people modifying your code won't change values they aren't supposed to change. Compiler optimizations can be made if the compiler knows a field's value will never

How could not using “final” be a security issue?

不羁岁月 提交于 2019-12-04 02:38:12
From page 113 of O'Reilly's Essential ActionScript 3.0 (2007): Methods that are final help hide a class’s internal details. Making a class or a method final prevents other programmers from extending the class or overriding the method for the purpose of examining the class’s internal structure. Such prevention is considered one of the ways to safeguard an application from being maliciously exploited . Does this refer to users of the API of a compiled, closed-source package, and "maliciously exploited" to learning things about the class design? Is this really a problem? For some more context, it

Unexpected output when using a ternary operator and final variable

最后都变了- 提交于 2019-12-04 02:21:06
Consider this code snippet: public static void main(String[] args) { int z1 = 0; final int z2 = 0; System.out.println(false ? z1 : 'X'); System.out.println(false ? z2 : 'X'); } When running this code, I would expect to see two X in your console. However, the real output is: 88 X If we take a look at the Java specifications regarding the ternary operator , we found that If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T. So the

Java build time constant configuration

别说谁变了你拦得住时间么 提交于 2019-12-04 01:58:06
问题 I have a project I would like to build using multiple configurations. I have a constant that needs to be different between builds but I do not know how to change it based on my config. For example I would like to be able to do the following based off a value in a config file. @WebService(targetNamespace = "http://example.com/") public class CustomerWebService { and @WebService(targetNamespace = "http://demo.example.com/") public class CustomerWebService { We use ant for building. 回答1: I would

Why instance variable to be final?

假如想象 提交于 2019-12-04 01:35:03
问题 I read this question about immutable objects and was left with a question regarding immutable objects and final field: Why do we need instance variable in immutable class to be final? For example, consider this immutable class: public final class Immutable { private final int someVal; public Immutable(int someVal) { this.someVal= someVal; } public int getVal() { return val; } } If in the above code there is no set methods and the instance variable is set only within the constructor, why is it

The blank final field INITIAL may not have been initialized

此生再无相见时 提交于 2019-12-04 00:54:16
问题 I'm programming in Java. I have added comments to every method to explain what they're supposed to do (according to the assignment). I have added what I know to the stub of Password.java (which I created after researching a javadoc provided by the school). My question is not about several functions, I know there are mistakes in testWord and setWord, but I'll handle that myself. My question is about this line: public static final java.lang.String INITIAL; This line is provided by the school,

Case expressions must be constant expressions for static final int?

狂风中的少年 提交于 2019-12-04 00:17:48
I have a final class Ring defined as: final class Ring { public static final int OUT = 3; public static final int MID = 2; public static final int IN = 1; } I also have a public class MorrisBoard with the following code: public class MorrisBoard { public static final Ring RING = new Ring(); private boolean checkMillBy(int ring, int x, int y) { switch(ring) { case MorrisBoard.RING.OUT: //... case MorrisBoard.RING.MID: //etc. //... } return false; } MorrisBoard.RING.OUT references a variable which is immutable for the lifetime of the program. All values are final. However, I still get the

In Java, can a final field be initialized from a constructor helper?

安稳与你 提交于 2019-12-03 22:02:41
I have a final non-static member: private final HashMap<String,String> myMap; I would like to initialize it using a method called by the constructor. Since myMap is final, my "helper" method is unable to initialize it directly. Of course I have options: I could implement the myMap initialization code directly in the constructor. MyConstructor (String someThingNecessary) { myMap = new HashMap<String,String>(); myMap.put("blah","blahblah"); // etc... // other initialization stuff unrelated to myMap } I could have my helper method build the HashMap, return it to the constructor, and have the