Possible Duplicate:
Usage of inner class
I was looking for main reason for which, inner class will be used, i got most common answer everywhere.. Due to re-usability of the existing code. But, we can achieve this by define one class outside of the class too. isn’t ??
class MyOuterClass {
private class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View view) {
mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
}
}
// later (inside some method)...
speakBtn.setOnClickListener(new MyOnClickListener());
}
here MyOnClickListener is the inner class, later creating the object same way as we do for normal class new MyOnClickListener(). So, what is the main difference, for the reason, it has been introduced.
The way you defined this inner class (non-static) allows to access members of the outer class. This means in general a rather private use within the outer class
An other reason to use inner classes is to make clear that the class is intended to be used by and with the outer class.
A static inner class could by accessed by any other.