25 June 2012

java inteview questions: topic : inner classes


Q. What are different types of inner classes ?

 Answer: Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types

·       Static member classes

·       Member classes

·       Local classes

·       Anonymous classes



Static member classes - a static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

Member Classes - a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.

Local Classes - Local Classes declared within a block of code and these classes are visible only within the block.

Anonymous Classes - These type of classes does not have any name and its like a local class

Q.Can an inner class be defined inside a method

Answer: Yes it can be defined inside a method and it can access data of the enclosing methods or a formal parameter if it is final .

Contributed by : Viji.V

 Q.What is ThreadLocal class?How can It be Used?

Answer: Below are some key points about ThreadLocal variables

# A thread-local variable effectively provides a separate copy of its value for each thread that uses it.

# ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread

# In case when multiple threads access a ThreadLocal instance, separate copy of Threadlocal variable is maintained for each thread.

# Common use is seen in DAO pattern where the DAO class can be singleton but the Database connection can be maintained separately for each thread. (Per Thread Singleton)
To find out more on ThreadLocal variable please refer the following links:

Article on ThreadLocal on IBM DeveloperWorks

Managing data : Good example

Refer Java API Docs

Q. What is the difference between sleep(), suspend() and wait() ?

Answer:  Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method).
A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread. t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.
object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist.

Wait is called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.


Click here for more java interview questions

No comments:

Post a Comment