final

What Could be the example scenarios to declare any class or any method as "final"?

What Could be the example scenarios to declare any class or any method as "final"? When do we need to do this? Please give me some example to understand the practical use of "final"... please elaborate your answer.... please guide me as I am a beginner in OOP ...

Implementing Clonable in Java

In which cases should I use this way: public A clone() throws CloneNotSupportedException { A clone = (A)super.clone(); clone.x= this.x; return clone; } And in which cases should I use that way: public ShiftedStack clone() throws CloneNotSupportedException { return new A(this.x); } What should I do if x is final and ...

Why is there no Constant keyword in Java?

I was trying to identify the reason behind the "CONSTANTS" in Java I have learnt that Java allows us to declare constants by using final keyword. My question is why didn't Java introduce Constant (const) keyword. Since many people say it has come from C++, in C++ we have const keyword. Please share your thoughts. ...

What's a reasonable way to mutate a primitive variable from an anonymous Java class?

I would like to write the following code: boolean found = false; search(new SearchCallback() { @Override void onFound(Object o) { found = true; } }); Obviously this is not allowed, since found needs to be final. I can't make found a member field for thread-safety reasons. What is the best alternative? One workaround is to define ...

Problems initializing a final variable in Java

I keep running into slight variations of a problem in Java and it's starting to get to me, and I can't really think of a proper way to get around it. I have an object property that is final, but dynamic. That is, I want the value to be constant once assigned, but the value can be different each runtime. So I declare the class level vari...

Is there a Pattern in Scala that add a method to an Array object?

Is there a Pattern in Scala that can add a method to an Array object? I am thinking of the implicit conversion of Int to RichInt. But that can't be done as Array is a final class. ...

What is the effect of final variable declaration in methods?

Classic example of a simple server: class ThreadPerTaskSocketServer { public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { final Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { ...

When and why does an EventHandler require objects to be final?

I have the following code from a GWT Project that is part of the onModuleLoad() method (similar to Java's main method, if you don't know GWT): final TextBox t1 = new TextBox(); final Label lt1 = new Label(); t1.addKeyUpHandler(new KeyUpHandler() { @Override public void onKeyUp(KeyUpEvent event) { // TODO Auto-generate...

Do the ‘up to date’ guarantees for values of Java's final fields extend to indirect references?

The Java language spec defines semantics of final fields in section 17.5: The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. I...

Quick Java question about private static final keywords for fields

I'm declaring a field: private static final String filename = "filename.txt"; First, does the order of private static final matter? If not, is there a standard accepted sequence or convention? Second, the filename in my application is fixed. Is this the best was to store its value? ...

Why this cache doesn't work using final as modifier

I have this code to get the Cursor once for this instance, and the Log shows it is called many times although I marked as final. What I am missing? private Cursor getAllContactsCached() { final Cursor c=this.getList(); return c; } getAllContactsCached method should retrieve list once, and the 2nd time it should re...

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

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

java serialization and final fields

I have an class defining an immutable value type that I now need to serialize. The immutability comes from the final fields which are set in the constructor. I've tried serializing, and it works (surprisingly?) - but I've no idea how. Here's an example of the class public class MyValueType implements Serializable { private final in...

Why do we make a copy of final instance variable in a method in Java?

Possible Duplicate: In ArrayBlockingQueue, why copy final member field into local final variable? For instance: private final Object o; public void doSomething(){ final Object local = this.o; //access methods of local; } This practice is followed in lots of java classes (such as ArrayBlockingQueue). What's the be...

Java Concurrency : Volatile vs final in "cascaded" variables?

Hello Experts, is final Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); the same as volatile Map<Integer,Map<String,Integer>> status = new ConcurrentHash...

final transient fields and serialization

Is it possible to have final transient fields that are set to any non-default value after serialization in Java? My usecase is a cache variable — that's why it is transient. I also have a habit of making Map fields that won't be changed (i.e. contents of the map is changed, but object itself remains the same) final. However, these att...

About local final varibles in Java

In java Program, parameters which is defined as String in method declaration. But in method definition it is accessed as final String variable. Whether it'll lead to some issues (like security, memory problem)? For Example: Method Declaration join(String a,String b); Method definition public void join(final String a,final String b)...

Assign final variable in a try block

Very short question: Is there a more elegant way to do this: Object tmp; try { tmp = somethingThatCanFail(); } catch (Fail f) { tmp = null; } final Object myObject = tmp; // now I have a final myObject, which can be used in anonymous classes ...

final and private static

I read that doing: public final void foo() {} is equals to: private static void foo() {} both meaning that the method is not overridable! But I don't see the equivalence if a method is private it's automatically not accessible... ...

Using a class as namespace

Are there any reasons I should not create a final class with static methods to avoid that some internal functions are called? final class ModuleGlobalFunctions { static public function generateWord { $result = ''; while (strlen($result) < 12) { $result = self::generateSyllable(); } return $result } static ...