Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inner classes and static initializers

Status
Not open for further replies.

Indrakumar

Programmer
Apr 3, 2002
1
AE
Hi,

What is the advantage of having static initializers and inner classes ? Is it left to my discretion to use it or is there any specific scenario in which i have to consider these ?

regrds
Indrakumar
 
Static initializers are important when you want to insure that parts of your object are accessible before they are actually instantiated. Or when you want to insure a particular order of when things get initialized. I've not yet had a use for static initializers. Inner classes on the other hand can be extremely useful, especially anonymous inner classes for when you only have one type of mouse event (for example) to respond to and you want to keep the code close to the object that uses the event:

JButton button = new JButton("Click me!");
button.addMouseListener(new MouseAdapter(){
//anonymous inner class
public void mouseClicked()
{
button.setText("I've been clicked");
}
});

This strategy is also good when you want to create a thread on the fly:

Thread t = new Thread(new Runnable(){
//anonymouse inner class
public void Run()
{
while(running)
{
//some code for thread here
}
});

t.start()

Hope that was helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top