final

Why would one declare an immutable class final in Java?

人盡茶涼 提交于 2019-11-26 14:19:44
I read that to make a class immutable in Java, we should do the following, Do not provide any setters Mark all fields as private Make the class final Why is step 3 required? Why should I mark the class final ? templatetypedef If you don't mark the class final , it might be possible for me to suddenly make your seemingly immutable class actually mutable. For example, consider this code: public class Immutable { private final int value; public Immutable(int value) { this.value = value; } public int getValue() { return value; } } Now, suppose I do the following: public class Mutable extends

Good reasons to prohibit inheritance in Java?

懵懂的女人 提交于 2019-11-26 14:07:38
What are good reasons to prohibit inheritance in Java, for example by using final classes or classes using a single, private parameterless constructor? What are good reasons of making a method final? Your best reference here is Item 19 of Joshua Bloch's excellent book "Effective Java", called "Design and document for inheritance or else prohibit it". (It's item 17 in the second edition and item 15 in the first edition.) You should really read it, but I'll summarize. The interaction of inherited classes with their parents can be surprising and unpredicatable if the ancestor wasn't designed to

java: “final” System.out, System.in and System.err?

青春壹個敷衍的年華 提交于 2019-11-26 13:07:22
System.out is declared as public static final PrintStream out . But you can call System.setOut() to reassign it. Huh? How is this possible if it's final ? (same point applies to System.in and System.err ) And more importantly, if you can mutate the public static final fields, what does this mean as far as the guarantees (if any) that final gives you? (I never realized nor expected System.in/out/err behaved as final variables) axtavt JLS 17.5.4 Write Protected Fields : Normally, final static fields may not be modified. However System.in , System.out , and System.err are final static fields that

Initialize a static final field in the constructor

ぃ、小莉子 提交于 2019-11-26 12:54:15
问题 public class A { private static final int x; public A() { x = 5; } } final means the variable can only be assigned once (in the constructor). static means it\'s a class instance. I can\'t see why this is prohibited. Where do those keywords interfere with each other? 回答1: A constructor will be called each time an instance of the class is created. Thus, the above code means that the value of x will be re-initialized each time an instance is created. But because the variable is declared final

final keyword in method parameters [duplicate]

南楼画角 提交于 2019-11-26 12:53:49
This question already has an answer here: Why should I use the keyword “final” on a method parameter in Java? 12 answers I often encounter methods which look like the following: public void foo(final String a, final int[] b, final Object1 c){ } What happens if this method is called without passing it final parameters. i.e. an Object1 that is later changed (so is not declared as final) can be passed to this method just fine Java always makes a copy of parameters before sending them to methods. This means the final doesn't mean any difference for the calling code. This only means that inside the

immutable class should be final?

让人想犯罪 __ 提交于 2019-11-26 12:29:34
问题 It says in this article that: Making a class final because it is immutable is a good reason to do so. I\'m a bit puzzled by this... I understand that immutability is a good thing from the POV of thread-safety and simplicity, but it seems that these concerns are somewhat orthogonal to extensibility. So, why is immutability a good reason for making a class final? 回答1: The explanation for this is given in the book 'Effective Java' Consider BigDecimal and BigInteger classes in Java . It was not

Java final modifier

穿精又带淫゛_ 提交于 2019-11-26 12:03:55
I was told that, I misunderstand effects of final . What are the effects of final keyword? Here is short overview of what I think, I know: Java final modifier (aka aggregation relation) primitive variables : can be set only once. (memory and performance gain) objects variables : may be modified, final applies to object reference. fields : can be set only once. methods : can't be overridden, hidden. classes : can't be extended. garbage collection : will force Java generational garbage collection mark-sweep to double sweep. Can's and Cant's Can make clone fail (this is both good and bad) Can

Final variable assignment with try/catch

放肆的年华 提交于 2019-11-26 11:28: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

Java : in what order are static final fields initialized?

 ̄綄美尐妖づ 提交于 2019-11-26 11:20:18
问题 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

The final local variable cannot be assigned

浪子不回头ぞ 提交于 2019-11-26 10:29:02
问题 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]; } }