Java Interview Questions

1.  What is the purpose of ‘final’,  ‘finalize’ and ‘finally?

final

final can be used to mark a variable “unchangeable”

private final String name ="foo";//the reference name can never change

final can also make a method not “overrideable”

public final String toString(){return"NULL";}

final can also make a class not “inheritable”. i.e. the class can not be subclassed.

public final class finalClass {...}
public class classNotAllowed extends finalClass {...}// Not allowed

finally

finally is used in a try/catch statement to execute code “always”

lock.lock();
try{
//do stuff
}catch(SomeException se){//handle se}
finally{
  lock.unlock();//always executed, even if Exception or Error or se}

finalize

finalize is called when an object is garbage collected. You rarely need to override it. An example:

public void finalize(){
     //free resources (e.g. unallocated memory)
     super.finalize();
}
Advertisements

Android – Handler, Message Queue and Loopers

Looper – part of any Thread to loop through message queue. This will be used to check if any incoming message has arrived to this thread. Only one looper exists for a given thread. Only for handler threads this looper will be activated, for other normal threads this will be in passive or inactive mode.

Message Queue – part of any thread, will store incoming messages to this thread. For any thread only one message Q is available. Handler – communication channel between two threads.

Handler is associated with Looper. for a given looper we can n number of handlers to communicate with it from out side world.