final

How to keep a "things done" count in a recursive algorithm in Java?

I have a recursive algorithm which steps through a string, character by character, and parses it to create a tree-like structure. I want to be able to keep track of the character index the parser is currently at (for error messages as much as anything else) but am not keen on implementing something like a tuple to handle multiple return...

immutable class should be final?

Hi, 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 reas...

When to use final

I've found a couple of references (for example) that suggest using final as much as possible and I'm wondering how important that is. This is mainly in the the context of method parameters and local variables, not final methods or classes. For constants, it makes obvious sense. On one hand, the compiler can make some optimizations and i...

Is there a way to implement algebraic types in Java?

Is it possible, in Java, to enforce that a class have a specific set of subclasses and no others? For example: public abstract class A {} public final class B extends A {} public final class C extends A {} public final class D extends A {} Can I somehow enforce that no other subclasses of A can ever be created? ...

Why can final constants in Java be overriden?

Consider the following interface in Java: public interface I { public final String KEY = "a"; } And the following class: public class A implements I { public String KEY = "b"; public String getKey() { return KEY; } } Why is it possible for class A to come along and override interface I's final constant? Tr...

Good reasons to prohibit inheritance in Java?

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? ...

Does using final for variables in Java improve garbage collection?

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 ...

Do you "final"ize local variables and method parameters in Java?

In Java, you can qualify local variables and method parameters with the final keyword. public static void foo(final int x) { final String qwerty = "bar"; } Doing so results in not being able to reassign x and qwerty in the body of the method. This practice nudges your code in the direction of immutability which is generally consid...

How to get a c++ constant pointer equivalent in Java?

When I pass an immutable type object(String, Integer,.. ) as final to a method I can achieve the characters of a C++ constant pointer. But how can I enforce such behavior in objects which are mutable? public void someMethod(someType someObject){ /* * code that modifies the someObject's state * */ } All I want is to prevent som...

Why should I use the keyword "final" on a method parameter in Java?

I can't understand where the final keyword is REALLY handy when it is used on method parameters. If we exclude the usage of anonymous classes, readability and intent declaration then it seems almost worthless to me. Enforcing that some data remains constant is not as strong as it seems. A) If the parameter is a primitive then it w...

Why avoid the final keyword?

In java, is there ever a case for allowing a non-abstract class to be extended? It always seems to indicate bad code when there are class hierarchies. Do you agree, and why/ why not? ...

creating final variables inside a loop

is this allowed in java: for(int i=0;i<5;i++){ final int myFinalVariable = i; } The keyword of my question is final. Is it allowed to do a final variable that changes with every run of the loop? I was wondering this because final says that you can't change the value of the variable (calling only myFinalVariable = i), but i'm redefin...

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?

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...

Lazily instantiate a final field

Is it possible to lazily instantiate a final field? The following code does not compile: public class Test{ private final Connection conn; public Connection getConnection(){ if(conn==null){ conn = new Connection(); } return conn; } } Is there an alternative? ...

Initialize final variable before constructor in Java

Is there a solution to use a final variable in a Java constructor? The problem, if I init. a final var. like: private final String name = "a name"; then I cannot use it in the constructor, while java first runs the constructor an then the fields... Is there a solution to get in contact with the final var. in the constructor? ...

override java final methods via reflection or other means?

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 ...

`final` keyword equivalent for variables in Python?

I couldn't find documentation on an equivalent of Java's final in Python, is there such a thing? I'm creating a snapshot of an object (used for restoration if anything fails); once this backup variable is assigned, it should not be modified -- a final-like feature in Python would be nice for this. ...

Why does C# not allow const and static on the same line?

Why does C# not allow const and static on the same line? In Java, you must declare a field as 'static' and 'final' to act as a constant. Why does C# not let you declare const's as final? I make the further distinction that in Java, every interface is public and abstract, whether this is explicitly declared or not. Aren't const's ef...

String and Final

What is difference between in the following statements String name = "Tiger"; final String name ="Tiger"; Although the String class is final class, why do we need to create a String "CONSTANT" variable as final? ...

Is there any way to declare final fields for Hibernate-managed objects?

I'm just getting started with Hibernate, and all the examples I'm seeing so far look pretty much like the tutorial in the Hibernate documentation: package org.hibernate.tutorial.domain; import java.util.Date; public class Event { private Long id; private String title; private Date date; public Event() {} /* Acces...