This topic is not for beginners or those at intermediate level. Not very difficult but may result in headache sometimes. So lets understand what are inner classes or nested classes - as some call them.
Inner class - as the name says are inside a class. Normal classes which we see are called outer classes because they are directly inside a file and not inside another class. Hey, if you are wondering whether a class can be inside a class?? Yes, it can be. But there are various complications in its use, and I suggest in real life not to use it unless they are unavoidable. But SCJP exam contains questions about such complex things..so its a very important concept. And hey its not boring, its very interesting topic... I will try to make it more interesting!
OK, let's see how a inner class looks like...
class A{
int a;
class B{
int b;
}//class B ends
}// class A ends
You can clearly see that B is inside A. When we create object of class A it doesn't mean we create object of inner class B also. We have to explicitly create object of inner class B. The inner classes can also be static! Yes, they can be. Also there can be more than one inner classes inside a class. You can create objects of them independently. Inner classes can have variables and methods as other classes... the members can be static, final etc.. The concept of Inner classes is not that simple, I thought to keep the first post easy, I will come with the difficult scenarios related to inner classes soon...
If you have any basic questions about inner classes, they do fire them in the comments section.. also try to code yourself, that is first thing to do when you don't understand anything.
No comments:
Post a Comment