- Podcasts
- The Java Developers Hub
- Java Nested Classes Explained | Static Nested, Inner, Local & Anonymous Classes
Java Nested Classes Explained | Static Nested, Inner, Local & Anonymous Classes
The Java Developers Hub
- December 3, 2026
- 1 h 21 min
- Episode 37
About this episodeJava supports nested classes, allowing developers to define one class inside another. This feature helps organise related code, improve…
- Understanding nested classes in Java.
- Why nested classes are important.
- Differences between nested classes and top-level classes.
- Understanding static nested classes.
- Creating and accessing static nested classes.
- Understanding non-static inner classes.
- How inner classes access members of outer classes.
- Understanding local classes declared inside methods.
- Exploring anonymous inner classes.
- Using anonymous classes to implement interfaces.
- Differences between static nested and inner classes.
- Understanding the relationship between nested classes and encapsulation.
- Practical examples of nested class implementations.
- Best practices for using nested classes in Java applications.
- Java beginners progressing to advanced programming.
- Computer science and software engineering students.
- Developers learning object-oriented programming.
- Backend developers building structured Java applications.
- Java programmers preparing for technical interviews.
- Anyone interested in Java class architecture and encapsulation
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Nested Classes Explained | Static Nested, Inner, Local & Anonymous Classes
Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to talk about a very interesting topic, which is called nested classes. Till now, we had read that we can nest loops, we can nest switch statements, etc. But in Java, you can also nest classes. And the use case of this is very important. When we understand about this, you will know that how many places we use nested classes. Rest, you will have a lot of fun, you will get to learn a lot of new things. So let's quickly go to the screen. Yes, so as we said that we are going to study about nested classes. So first let's understand what the concept of nested classes is. So till now, whenever we used to write a class in Java, let's say we wrote class A. And here we used to write some code in that class. We used to declare some variables. We used to write some methods etc. Now you must be understanding from the name of nested classes. That it must be that in this class A. We would have written one more class. Let's say B. Something like this. Now, the B class that we have written inside A, what kind of class is it? According to that, we divide nested classes into four parts. Okay, let's see what parts they are. But I hope you got an idea of what we are going to talk about in nested class. One class within another class. So, we will generally call the outer class as outer and the inner class as inner class. Now, obviously, there is no level of nested. If you want, you can make another class C inside it. You can make another class D in it etc. But generally, when we talk now, we will talk on two levels, an outer and an inner class. So first of all, let's see which are the four types of nested classes that we have. So let's see it once. So nested class is of four types. One is static nested, one is called inner class, one is called local class and one is called anonymous class. Now, how are these four parts divided? They are divided in such a way that how the inner class is nested. Now, when we will understand one by one, you will understand. But first, the most important question, if we think from the first thought principle, then why do we need a nested class? Why do we want a nested class? There should be some logic in one class and in the other class. So, obviously, there is logic, right? If you think carefully, when we nest a class, the biggest benefit you get is logical grouping. What is logical grouping? When we were writing code, we used to write that we will first make a class A, we will do some work in it. If we want to represent another entity, we can create another class, let's say B, and work on it. Similarly, we can create any number of classes, let's say A, B, C, etc. But imagine that I want a class which I know doesn't make sense if it exists individually. It should logically exist within a class. If we think about it, the class makes the logic of being within a class. When you look at its examples, or in real life situations, when you come to a use case, when it says business logic, here you will find out that there is an inner class requirement. Because if that class exists outside, then there is no problem. But there is no sound that it is logical. So logical grouping means that a class which is meaningful only if it comes under another class. So a class is our outer class and another class comes under it which we call inner class. Now this inner class makes sense only when it comes under outer class. This is called logical grouping. So the next reason for nesting a class is that we get better access to the outer class. So let me write it like this. Better access to outer class. Okay. Now, what does it mean that better access to outer class? It means that whatever is our inner class, it can also access the private members of the outer class. Okay, how? Look, imagine that we have a class outer. This is a normal class that we make from the beginning. Now, there must be some private fields in it. Let's say I have kept private. int x equal to 4. We don't declare it here. So I have written private int x here. So what I said is that whoever has a private access modifier, who has the authority to access it? Whoever is in this class. If there is a method in this class, let's say void fun, that method can access this private member. Because what does private mean? That it can be accessed in the class. Now imagine our inner class. So if I make another class in it, class inner. Now, this class is also lying inside this outer class. So, logically, this class will have access to this x. So, basically, this inner class, to take access to this x variable, we don't need getter setter. If we write this inner class outside, class inner, somewhere outside, on the level. Now, this inner class, If this x variable had to be accessed, then what would it do? It would depend on the outer that the outer gives me some getter setter. So, if I have a method by doing get x, then I can access x. Or if it is set x, then I can change some value in it. Right? So, basically we understood that yes, the inner class can also access private members without getter setter. So, this is also a benefit. So, there are many benefits. You will understand them gradually when we will talk about that particular class. So, any class can make sense only when it comes inside another class. Or it is nested, right? So, that's why we can nest classes. Or the second reason is that it wants to access private members. Okay? Now, first of all, we will start with our static nested class. Static nested class. Now, you must be able to understand by its name. Static nested means that our inner class will be static. There will be a static keyword in front of it. So, basically, our class will look like this. We will have, let's say, an outer class. Which I am calling outer. And in this class, I will have a I will make a class which I will call inner And in front of this inner class there will be a static keyword Something like this static class inner So if I write it carefully So something like this So our inner class will be static and outer class will be normal Now how will it work, what will be its properties, how will we make its objects We will talk about all that one by one So let's go directly to the code and see it there So I have again made an empty project Public class demo Now what I will do first of all I will make a class, let's say class class outer ok we were talking about this now because I want to make a static nested first I will write here this is an example of static static nested class ok so inside this I will make a static class static class let's say I call it inner something like this so in this way I can make a class an outer class and inside that an inner class which is static class ok now before moving ahead one most important question How will I make the object of this inner class? Right? First of all, let's answer this and then we'll see its properties. First, how did we make the object of outer class? We used to make it normally. Let's say, we used to write the class name outer. Let's say, small outer. Equal to new outer. This is how I used to make an outer object. Right? Now this is my outer object. Now with this outer reference, I can access any method or variable of this outer class. Whichever I have permission to access. Okay? Very good. But how do I make an object of static nested class in this? Okay, first of all, static keyword. We know that in front of any method or any variable, when we put static in a class, let's say in this class, I have written a variable, static int x, let's say this variable. So, I know that this x does not belong to any object, it belongs to a class. We have read that this becomes a class member, not an object member. And this is common for all objects. So, all the outer objects that we make, all of them will share this x. So, the same truth is there about inner class. Now if you think carefully, what is this inner class? It is a block which comes inside the outer class, but it is a static block. This means that it will be shared among all the objects which we will make outer. This will be associated with this outer class and not with any object. So if we want to make this object of inner class, for that we don't need the object of outer class. So for example, again I will take the same variable, let's say static int x. And if I wanted to access x, I could have done outer.x. I don't need any object of this outer. Similarly, if I want to access this inner class or make its object, then I don't need the outer object. Okay, first thing is fine. Let's keep this aside and comment on it. Second thing, Now you think, what will be the name of this inner class? Can I write inner directly? And will my Java get this class? Absolutely not. Because this inner class is not declared at the outer level. We know that all classes come in a package. So if JVM wants to find a class, then it can find a class from that package name. PackageName.ClassName But this inner class, this is a... And the class lies inside the outer. So shouldn't its name be something like this? outer.inner. Absolutely right. So first of all, JVM will go to outer.inner. So it will see if there is any inner class inside this outer class. The answer is absolutely there. It will get the inner class from outer.inner. And in this way, JVM can access it. That is, whenever we say the name of this class, we will say outer.inner. Okay. So we have to make an object of outer.inner. So let's say we keep a reference variable of inner. Now I will write a new keyword here. And again the name of my class. Now what is the name of my class? The name of my class is again outer.inner. and my parenthesis. So this is how I can make my inner class object. I didn't get any compiler error, just a warning that I didn't use inner. Apart from that, I didn't get any compiler error, which means this is the correct syntax. So we saw that we made the object of this inner class without making an object of outer class because this inner class is static and it is not associated with any outer object but it is associated with the whole class. Okay, very good. If we have made an object, then we can call its method also. For that, I will make a method named void fun which does nothing. System.out.println Let's say it only prints hello on the screen Okay, hello Something like this Okay, the spelling is wrong here, so system.out.println Hello, okay Now see, I have declared an object here, whose reference variable is inner So now if I want to call its method, then I can do it directly, inner.fun And if I run it, then hello should appear on my screen, which it did. So this way I can call its methods too. Now if I want, I can write as many methods as I want. For now, I have written only one method, fun. You just remember in your mind that I am doing all this without making an outer class object. Because this is a static class, our inner class is static, so it doesn't need an outer object. Now let's look at some of its properties. The first property is that since we have made the inner class static, it cannot access the non-static members of the outer class. Non-static members can be any variable, any function or any method. Why can't it? We had read that if we make a static method in a class, let's say I said static. I'll create a method here named greet. Sorry, I didn't write void return type here. I created a method like this. Since this method is static, it can only access static variables. This is the rule of static. Likewise, this class is also static. So what can this class do? our outer class can only call static methods or can access static variables. So if we have a static int x and I put its value here, 4, then it can access this value. Let's say it prints this variable x first. So our variable should be printed. Let's see once. Our 4 is printed. That means it can access the static variable. But if I had a non-static variable here. Let's say a simple non-static variable int y. So, would it be able to access this y? So, if I write system.out.println and here I write y again. Now, obviously, I have not put any value in y. So, our default value should come which is 0 in the case of int. So, would it be able to access this? Not at all. Here, I got an error. Cannot make a static reference to the non-static field y. So, now we cannot access a non-static method or variable from a static class. Now, you already know the reason for this in the case of functions. But, let's talk about it again. What would be the reason that we don't want a static class to access a non-static method or its variable. It's a very simple reason. As we saw above, we can make an object of static nested class without making an object of outer class. This is what we did. We wrote outer.inner inner equal to new outer.inner. How did we access this class? By doing outer.inner and we made an entire object of it. Without making an object of outer class because this class is not linked to any object, it is linked to the entire class. Now, if any inner class method tries to access any non-static variable or non-static method, like in this case we tried to access y, then obviously there will be an error, because you have made an inner object without making an outer object. And in this case, this y which is non-static, This is linked to an outer object. So if you didn't make an outer object, how can you access this y without an object? Can I access this y without making an outer object? The answer is no. Because this y is linked to an object. So in the same way, I can't access it here either. Java won't let me do that. Because this y means which y? Which object's y? We haven't made an outer object yet. For this reason, we can't access any non-static variable in a static class. We read the same reason if we make a static method. Let's say here, a static object. method exists, static void fun, again, let's keep the same name, let's say, okay? If a method exists here, then this method also can't access this y. Why? Again, this is a static method and this is a non-static variable. Okay. Isn't there any way to access this y? There should be a way, right? Otherwise, won't we ever be able to access a non-static variable? There is one way. Look, what happens if this function takes a parameter and what does it take in that parameter? The reference of this outer class. So, basically, what I am saying is that if I take a parameter here of outer, its name is outer. So, can't I do outer.y here? Now when I call this inner.fun, I have to give a reference of outer. But I haven't made an outer class yet. So let's make an outer class first. Outer. Let's name it smallOuter equal to newOuter. Something like this. So we made an outer class. Now in this method, I have passed the reference of this outer. See, my error is gone. Now, will I be able to access it? Will both x and y be printed? x should be 4 and y should be 0 because we have added default values to it till now. It is 4 and 0. Very good. So, basically, we can do this. There is one more way to do this. Let's make a constructor of this inner class. Let's say I said inner constructor. Okay. And expect an outer class reference in this constructor. Something like this. And what can we do? Obviously, we can put a member here. Outer. Outer reference. and we will write this.outer equal to outer I hope you understood what we did We made an inner class constructor which expected an outer reference and we had another variable again we had an outer reference and we put this.outer in this outer whatever is coming in our parameter Now I don't need an outer in this function I can simply call outer.y through this outer. Now what I have to do is, instead of passing the outer here, I have to pass the outer reference here. Something like this. I still don't see any error and if I run this code, Then my same output will come. I hope you understood this. You know we can make such constructors in which we can take reference of someone else. Like we took reference of outer class. So we can do this or we can pass directly in that function. Now we talked about some properties of static class. The most important property of static class is that it can only access static variables of outer class and not non-static. But as we said in the beginning, interestingly, let's say if this variable is private, private static, then also there would be no problem, I would be able to access it, because I can access private variables also, because this inner class is coming inside the outer, and whatever is inside the outer, everything can access this index, absolutely great, so we can see this too. Now whether I make it private or public, it can't access y directly, it can do it indirectly as I have accessed it, okay. Now, let's move forward and see what other properties of this inner class are. Now, you have understood that it can only access static members and not non-static. Now, whether it is a variable or a function, if there is a function in the outer class which is static, the inner class will be able to use it. Otherwise, if it is non-static, it will not be able to use it. Okay. Apart from that, our static class is like a normal class. It can extend a class. So, I can write here, let's say, extends and some other class, let's say, demo, okay, so see, we had a demo class on top of us, it accessed it, so we can do this, it can also implement an interface, so I can write here implements, now we haven't made an interface, but if something had been made, then let's say, if there was an interface called demo2, then I could have implemented it, your inner class will behave like a normal class, it can extend any class, it can implement any interface, you can make any methods in it, normally, like I made a void fun, I can make any other methods, I can declare any number of variables here. Let's say I declare a string name here. So I can do all this. We have seen that there can be a constructor in it. So the normal class behaves in the same way. Its name changes. Whenever we want to call it, we will call it outer.inner. We can't call it inner directly. Okay, one very important thing. Can there be static methods or static variables in our inner class? Can I call it static int? Let's say number. Can I write something like this? Did I get an error? No. Can I make a method like this? Fun2. Let's say. Let's do fun2. Can I do this? I don't have any error, means I can do it. In a static class, I can keep static members, whether it is a variable or a method. This means that the inner class can do everything that a normal class can do. Normal means whatever we have studied till now, which was not nested in anyone. Now, because there can be a static method in the outer, so it can also be done in the inner. The only thing that changed was the name outer.inner and one more thing changed that it can't access the non-static methods of the outer class. Apart from that, everything is the same. Okay. Now, the existence of static methods or static variables is important in a nested class because when we study the other nested class which is the inner class, it can't do this. It can't keep static methods or variables inside it. And we'll understand the reason when we read both of them. But for now, you understood how we made this nested class and how we did all the things inside it. Now, the next question. In our inner class, all the variables and methods that we write, can we put an access modifier inside it? Can I write it private like this? The answer is yes, you can write it. There will be no error. See, there is no error. There is just a warning that the value of field outer.data name is not used because you haven't used it yet. Simple. Apart from this, I can make it private too. Similarly, I can make it protected or public too. Okay, very good. So, what I mean to say is that I can use access modifiers too. In fact, using access modifier is a big advantage of this. How? So, what do I do for that? I will show you an example. For now, let's say you forget this class. This outer and inner class. this outer and inner class. Let's work on a new class. Let's say I have a class called bank account. Now imagine I have a public method in this bank account. Let's say public void compute interest. Which gives us interest of any principle. So let's say I take double principle amount in our parameter. Now here, I can put interest calculation logic here. But I want to keep multiple types of interest calculators. One will be yearly interest calculator, one will be monthly, one will be quarterly. I want to keep many types of interest calculators. So, another way is that I make different methods of all of them and call any method here. Or what I can do is I can make an internal class, a nested class. So, what I will do is nested class, static class, let's say, interest calculator. Interest Calculator class I have made here So here I have made a static class called Interest Calculator Now you think, how many helper methods can I put in it? Let's say I have put one method, double Calculate Yearly Calculate Yearly What will it do? Let's say it will take a principle, double principle principal and let's say your rate will calculate at which rate and will give you the final answer return principal dot rate Imagine, this is how they gave us the principal into rate. Now, let's say I make this method private. That means, no one else can use this method other than this bank account. Now, imagine, with this compute interest method. Okay, its name is computer interest. Now, with this compute interest method, if I want to call calculateEarly, how can I do it? Now, see, because this calculateEarly method is not static itself. So, although the class is static, but the method inside it is not static. So to call this method, either I have to call its object or I declare it static. Now I don't need to call its object. Now if I want to call this calculate yearly, then how can I do it simply? I can write, let's say return whatever our interest calculator class is, interest calculator. The interest calculator class will give dot Calculate Yearly I have passed the principle on the principle And on the rate I have passed 9% So 0.09 Okay, there is an error here I have avoided it I will have to double this because we are returning. So see, I didn't get any error. So think what I did. I made a class of bank account first. In that, there is a method called compute interest. Which takes a principle and calculates some interest. So either I could have written this entire logic principle into rate in it. But I thought that I will need a lot of logics like this. Now it is calculate yearly, tomorrow I have to calculate monthly, quarterly. I have to make it in many ways. So I do it, I keep multiple methods. And I put all those methods in a class. Because it is all logical grouping. So I made a static class called InterestCalculator and put a method in it called calculateYearly. Now I can put another static method in it called staticDoubleCalculate. Let's say calculateMonthly. I can do something like this. I can also do something like this. So let's remove this for now. If I make this class private, will anyone see this class outside the class? No, because it is a private member. So this class will be hidden for everyone. Now think, who will have access to this class? This class will have access to every method that is written in this bank account. For example, compute interest. That is, this interest calculator class will work like a helper class for this compute interest function. Because no one else can see it. So interest calculators are hidden from the outside world. Who can access this class? All the methods of this bank account class, like compute interest, interest calculated and calculate yearly. Tomorrow I will change the logic and make a static double calculate monthly method. Then if I want, I can do interest calculated and calculate monthly. Or I can make more such new methods that can call the methods of this class, but no one can call this class. No one can use this class because the class is completely private. So I can do this too. I can make the whole class private. I can make some variables private in this class. Let's say private index. I can write like this too. I can do anything which I can do in a normal class. But yes, I can make the whole class private too. And this is the best option. Thing I can do. Because what benefit do I get from this? I hide a whole class from the outside world. Because this class only makes sense inside the bank account. Interest calculator doesn't make sense in itself. It works as a helper class for the bank account. So if this is a helper class, then why should I write it separately outside? I write it inside this. Right? So I hope you understood how interesting it would be if we deal with nested class. So I hope you understood all these things. Now if I talk about some use cases of this. Use cases. use cases of static nested class, then we will get a lot of things. So let's see them one by one. See, the best use case of static nested is that if our nested class acts like a helper class from our outer class, then it has a very good use case there. As helper class for any outer class. And the second use case is in the builder design pattern. Now what is the builder design pattern? Again, this is a design pattern that we use a lot. If you want to watch a video on this, then you can click on the i button. I will attach the video of our builder design pattern. Our static nested glass is also used a lot to make this. Now, what can be the third use case? Now I have told you that our static nested class, this one, let's go above this, our this one, this static nested class, we can keep static methods inside it. Which we can't keep inside the normal inner class. So if you want a nested class in which static methods can be there, then also you will have to use static nested. So basically, if you want to have static methods inside a nested class, I hope you have understood all the use cases of this. There is one more use case of this. But you will understand it better when you will read the Spring Boot series. Whenever we make a request response. We make a DTO of a request response. If you don't know what DTO is or what request response is. No problem. When we will start the Spring Boot series. You will understand. In that case also. Static nested class is used a lot. Let's not talk about it now. It will be a little complicated. But if we talk about it in a normal way, then if our inner class is simple, if it can do the work of a helper for the outer class, then you can use static nested class there. Apart from that, we saw that static nested class behaves completely like a normal class. So here I have written all the properties of our static nested class. You can easily go through it. Now let's move towards our second type of nested class which is inner classes. So what is our inner class? Any nested class which is not static is called inner class. So basically we will have a class outer. And the nested class we are going to put in it will be non-static. So we will simply write our class Inner without static keyword. And this is called inner class. Now again let's go back to our code. Let's see some properties of this. You will enjoy it a lot. Yes. So first of all I will write here. We are going to talk about inner class. So again I have taken an empty class. Okay. In the name of demo2. We talk about inner class here. So again what did we read? Let's say we have an outer class. Inside that we will have an inner class. Class inner. Something like this. Here we will not put static keyword. Now, if you think carefully, this inner class, because there is no static keyword in front of it, right? So, this class will relate to the outer object, right? The outer object will belong to the class. Now, how will we make its object? Now, we can't make its object until we can make the outer object. So let's see how to make an object out of this because it is very interesting. So what I said is that its name will remain the same, outer.inner. Now let's say I name its reference variable again inner equal to new. Now what should I write here? Can I write like this outer.inner? Something like this. I can't write it, I have compiler error obviously Because our inner class needs an outer object to declare Because it is not static So first of all, I have to get an outer object Let's say small outer equal to new outer So now I have made an outer object Now because of this object, I can declare this inner class How? Now this syntax is completely wrong, I will comment it The real syntax will look something like this that I will say outer.inner class name will remain outer.inner. We will keep the name of its reference variable inner equal to. Now, here I write new keyword, right? To declare any object in memory. But you think, can I write directly new? If I want to make the object of this inner class, then I will not need this outer object. For example, if we ignore this for one second. Assume, here is a variable int x. Now, if I had to access this x, then how would I write? Let's say, I want system. Out.println So I used to write like this Outer.x Something like this Right? So the x would get printed So what did I do? Object's name Then dot operator And whatever I want to access So I used to do like this So if you think If I remove this x again If you think if I want to make this inner object, then won't I have to do this? I will write outer dot. Now whatever I want to do, let's say new inner dot. Something like this. So, this is the entire syntax. So, again, what I did first, the class name, which is outer.inner, will always be this. The name of the reference variable, which is inner, equal to. Now, I can't write directly new inner, right? Because JVM won't know which is the inner class. It doesn't come directly in any package, right? It comes in an outer class. So first of all, we will take an outer object and put a dot operator on it. Whenever we use the dot operator, we go to heap memory and see what we have to access now. We will get an inner class inside the outer object. To make its object, we will write dot new inner. Simple. This syntax is very logical. You must be making sense. that what we always do with dot operator, we look inside the object that now what I have to access, now if I have to access any x, then I write outer dot x, if I have to access any function fun, then I write outer dot fun, right, in this case, I have to access this class, and to access means to make the class object, so I wrote outer dot, new inner, okay, now if I wrote like this, now you think, in this inner class, I have a method, let's say void fun, let's take the same method, which does nothing, let's say hello print, so system dot out dot print talent, So let me simply print hello here. To call this function, I'll simply write inner.fun. And I didn't get any error here. If I run it, then hello is printed on the screen. Simple. So this is how inner class works. If I want to make an object, then I have this syntax. First of all, I made an outer object and then I made an inner object using it. Java gives us a special syntax which allows us to do both the tasks in one line. Something like this. If I write outer.inner which is the name of our inner class and its reference variable which is inner equal to new outer dot new inner. So I can do the same in Java. See, I didn't get any error. And if I run inner.fun, I will get the answer, hello. Very good. Now, as soon as I did new outer, the first thing that will happen is that an outer object will be created in our heap memory. On that object, I directly put. newinner. Now, you know what dot means. Let's look inside the object. What we have to do is access the inner class. And to access it, we have to make it an object. So I directly wrote. newinner. So again, our inner class will be an object. Now think about it carefully. When I did this, I directly assigned the object of inner class to our inner variable. That's why the class name is outer.inner. But I didn't refer this outer class object to anyone. It doesn't have any reference variable. So we will use this syntax only when we don't need the outer reference variable. We just have to use the inner class directly. In that case, we can use this syntax. Otherwise, obviously, this syntax is better. That you first make the object of outer and then make the object of inner using the outer object. So, I will comment this for now. Okay, I hope you understood this. Now, let's go to iPad and see some of its properties. Yes, so before moving ahead, let's see some of the properties of inner class, which is a simple inner class. I have a question for you, okay. Now, as we saw in the code, what did we do? If I write the same syntax again, then we first made an object of outer. Let's say outer equal to new outer. Something like this, right? And after that, we used this object, this small outer to make the inner class object. So how did we make the inner object? First of all, we wrote its name, which is outer.inner. This name will always remain the same. The name of its reference variable is inner. Equal to, we wrote outer.newinner. Something like this we wrote the syntax. Okay? I hope you understood this syntax well. This particular syntax. Now you think, what will be happening in memory? We have a heap memory. When the first line is executed, we know what will happen. As soon as we write new outer, a space will be allocated in the heap memory for the outer object. Similarly, we have a heap memory and a stack memory. There will be an outer reference inside this. And we have assigned this to it. We know that this work has been done before. Now think that we have outer reference and object. Now what will happen in the next line? We have to make an inner class reference variable which is outer.inner. So the reference variable here will be inner. We know this. But what is the meaning of this line? First of all, we have written outer dot. So we must have gone to the reference of this outer object. Do you know what dot operator does? Dot operator means if you put a dot on a reference, then we start looking inside that object. So we have seen the outer dot inside it. We have to make an object of a new inner class. So will this new object be created inside this object? Something like this. And it will point to this that it must be like this. The answer is no, it won't happen. Actually, we will have a separate block inside the heap. Something like this. And our inner will point to this block. And this will be our inner object. But we have written something like this, outer dot. And we had also read that our inner class comes under outer class. So why are its objects created separately? Here our work comes, this keyword. We had also understood that every class has its own reference, which we call this. And when we understood the size of the object, we saw that in its metadata, the reference of this disk is also stored. This will also surround our memory. Whenever we make an inner class, something like this. If you look at this code carefully, whenever we made an inner class, So, in this inner class, there is always a reference of its outer class. If it is non-static. Obviously, if it was static nested, it would be a static class. It doesn't need a reference. But again, if this non-static class is a proper inner class, there will always be a reference of its outer class in it. So, suppose JVM writes such a code from inside. Outer, small outer. So let's assume that the compiler will write from the inside, you don't need to write this. But an outer reference will always be present in the inner class. This means that whenever you create an inner class like this, it must also have an outer reference from the inside. It must be stored. Right? That's why both the objects will be different in the heap. It won't happen that the inner class object will be inside the outer object. No, it will be in a different heap. But yes, this inner object will have a reference of this outer object. I hope you understood this. If you understood this, then let's go back to our inner class. We saw that we have a class called outer and inside that we have a class called inner. Since this is a non-static class, it can access everything of the outer class. Now if we talk about everything, everything means that what all can she access? If there is a static member here, she can access that too. and non-static member can also be accessed. So, the static and non-static member, whether it is private, public or protected, it doesn't matter to the inner class because it lies inside the outer. It can access both private and public. So, the inner class doesn't have any limitation. In our static nested class, it could only access static variables. This static method, non-static method, static variable, non-static variable can be accessed by everyone. Now, you already know the reason why it is able to access everyone. Because we know that the inner class is linked to an outer class object. Until the outer class object is not formed, the inner class object will not be formed. So imagine that this is your outer class object. So let's say this is an outer class object. For this, specifically you will have an inner class object. So let's say this will be the inner object. If you make another outer object, for that you will have a different inner object. So basically, for every new outer object, there will be an inner object of its own. These two will be connected to each other. Like not static nested, there will be multiple outer objects and everyone will have the same inner object. Why? Because it will be shared, because it is a static class itself. So you will have to remember this. Now imagine, let's say I have an outer class, class outer. And I have declared a variable in it. Let's say int x equal to 10. Suppose. Now I make an inner class here. Class inner. And if I put the same name variable here. So int x equal to let's say 20. Now when I will print it as system.out.println x. So what will be printed? The answer is 20 will be printed. Why will we get 20? Because whenever our inner class sees that the same variable is present in the outer and the same variable is present in the inner, then it always gives priority to the object of the inner class and the upper one becomes a shadow. So when we print x, we will always get 20. Now if I want to print this x, then what should I write here? In system.out.println, what should I write here that my upper x gets printed? The answer is that I will use the reference of outer in this inner class. So I will write a code that I will write outer which is the name of my class, dot this. So I used outer's this and dot x. Now if I write this, then my outer's x will be called. Let's quickly see these things in code. So I told you, let's say I have a variable int x equal to 10. And let's say I also have something in the inner class, let's say int x equal to 20. Now let's say I print x instead of hello in my fun function. So what will I print? If I am calling inner.fun, then on my screen, 20 will be printed, which we had expected, right? It has shadowed the upper X. But if I want that apart from 20 prints, from 20, then I can write something like this. outer.this.x. Sorry, this.x. See, I didn't get any error. What does outer.this mean? I accessed this from the outer class. You know this. It holds the current reference of the class, so it will hold the reference of the outer.. x. I accessed its x. So if I write it like this and run it, then I get 10 too. Very good. Okay, if I hadn't written int x here, and in that case, if I had directly accessed only x, then my simply upper x would have been printed. So if you want, So you can run it and see that 10 has been printed. How did it happen? I told you that from inside, the inner class has something written like this. Outer, outer. There is a reference of outer. This compiler writes it by itself. You don't need to write it. So when you write this x, the compiler changes it from inside to outer.x. It changes it to this outer's x. So again, you don't need to write this. It is implicitly present. You just need to write x directly here. And your variable will be printed automatically. Okay? Now all the properties of our inner class are the same as that of our static nested class. Except what we just talked about, that inner class can access any member, whether it is static or non-static. But our static nested class could only access static members. Now there was a difference between the two that we talked about earlier. In our inner class, static methods or static variables cannot exist. So if this is true, then basically I cannot write this. If I make a static method, static void fun, This should give me a compiler error But see, it didn't give me any compiler error, my code is running, right? Means there is no compiler error So if I want, then here also system.out.println I can print something, let's say hello And my code should run, see, there is no error Now obviously because this is a static method So what can I do? I can either call it by doing inner.fun2 I can write the syntax like this Or I can write like this, outer.inner. fun2 Something like this So if I run the upper one once So my hello is printed with 10 And if I run the lower one too So my hello should be printed twice simply Okay, yes, hello has been printed twice. There is a warning here, it always comes, that it is a static method, so why are you calling it with object, you try to call it directly with class name. Okay, again, that warning doesn't matter, but the matter is that I can do this now. But Abhiya, you just said that static method cannot be in an inner class, and this separates it from normal static nested class. This was absolutely true up to Java 16. Before Java 16, sorry, Java 16, Before Java 16, it was true that static methods are not allowed in any inner class. But since I have a Java 16 updated version, I am not showing any error here. Now, if it was not allowed before, then we will have to understand why it was not allowed and why it is allowed now. Now, for this lecture, we will assume that if we are reading old Java, then we will understand that static methods or static variables were not allowed in the inner class. So, I couldn't write anything like this static int x equal to 10. This was also not allowed. But see, now I don't know any error, now I can do it. So, why was it not allowed earlier? We will understand that in this lecture. But later, when we will read the latest versions of Java, then we will also understand that how Java allowed it. Okay. Then you will understand it better. So, now let's go once and understand the same thing that why Java used to say that static methods or static variables cannot be in the inner class. See, yes. So, we said that whatever is our inner class, In it, Java did not allow static members before. It was not allowed. Why it was not allowed, we have to understand that. If you see the documentation of Java before, it says that normal inner classes cannot have static members. To understand that, first of all, let's make a class. Let's say class A is my external class. And in it, I have a nested class, class B. Something like this. This is my inner class. Now you think, what does static keyword do in front of any member? Whether it is a variable or a method. Static means that it belongs to the class and not to any method. It belongs to the class and not to any object. This is what it means. So imagine, I will take an example of a variable. If java would allow static int x Suppose a variable is declared x and I have made it static. Suppose first java says that it is allowed. So what is the problem? The problem is that you want to say that this x variable belongs to B class and not to any object of B. Right? Okay. But this whole class B itself belongs to A object. Right? Because this is not a static class. We had understood that class B has This A has a reference internally. That means, for every A object, an object of B will be made. For every A object, an object of B will be made. Right? An object of A will be made. So, this class itself is associated with the object of A. But we are saying that in this class B, there is a static variable called X. That is associated with the class, not with any object. Now, Java says that this should not be allowed because it creates a confusion. And that confusion is that the X variable that I have, Should it be different for every a object or should it be the same for all a objects? Let's say this is an object, this is an object, this is an object, this is an object. There are a lot of a objects. These are all a objects. Should my variable x be the same for all? Because since this is static, right? So, we will simply think that yes, it should be the same for everyone. But again, the class B is associated with an A object. That means, for this A, we have an object of B. For this, we have an object of B. For this, we have an object of B. For this, we have an object of B. Since we have a B object for every A object, then should the X be the same for all of them? Right? This is a confusion. Now Java removes this confusion by saying that I won't let you do this. If you want to declare a static variable in a nested class, then that nested class should be static. That means you should write static here. We will call it static nested. which we had read before. The old Java didn't allow you to do this before Java 16. Now that confusion is gone. How did Java remove it? We will read that in modern Java when we will see its videos later. But I hope you understood from where that doubt came in the mind of Java. That this x itself is static and static means that it should be the same for everyone. So does every instance get only one x? But since this class is different for every instance, should this x also be different for all? So this was a big confusion. Java will solve this confusion. Simply, I will not allow this. You can neither declare a static variable nor a static, let's say, void, fun method inside. Both of these were not allowed in old Java. I hope you have cleared this doubt. Now let's move on to our next class, which we call local class. Now this is very rarely used. Its use case is very less. You might see a local class being made on production. But because it exists, we will have to understand about it. It is very interesting. Now see what local class says. Till now, we had seen that you had written a class directly inside the class. So, local class is any class which you have made inside a code block. So, let's say you have made a class here, local. Something like this. Okay, now which should be this code block? This can be a code block of any thing. This can be a code block of any if. It can be a for loop code block or a switch code block. If you make a class in any code block, we call it a local class. In fact, you can make a class in any function. Imagine how interesting it is that you can make a class in any function. The syntax will look something like this. Let's say I have a function void fun. Let's say it was written in the main class. So I have a fun function. If I want, I can make a class in it. Let's say local. I can name it whatever I want. For now, I have kept it local, L capital. Because it is the name of the class. Yes. So I can do this. This is allowed in Java. And not only in function. I can make it in any if statement. In switch. In fall loop. In while loop. I can make it in any of them. And this is our local class. So let's see its example. By going to code. So again. See what I have done here. I have done demo 3. And made the project empty. So let's say. We first make our class separately. Class outer. Something like this. Before that. I mention it here. So who are we going to talk about here. We are going to talk about. Local class. Local. So we made an outer class Let's say this class has a method voidGrid Which doesn't do anything, it just says hello And if I want I can write system.out.println And here I can write hello So I used to do like this And if I wanted to print hello, I used to get this outer object Let's say outer outer equal to new outer And I would simply do outer.greet Something like this So my screen would get a hello print So if I run it, my screen would get a hello print But if I want, I can make a new class in this method as well I can declare a class in here, let's say local Something like this And in this, let's say I have made a method. Say hello. And here I have printed hello. Actually. Here. Okay. I will remove it from here. So, I can do something like this. I made a local class. Inside that, I put a method sayHello. And in that, we printed hello. Now, in this method, in this greet method, if I want to actually print hello, then what will I have to do? First of all, I have to make this local object. Let's say local local equal to new local object. Something like this. And then this local variable will have to be called sayHello. Something like this. Now see, what will happen now? First of all, my outer object will be created. Then I called outer.greet. My flow of control came inside this method. Now as soon as flow of control came inside this method, It has declared a class. Okay, great. The class is declared, no problem. Compiler is always concerned about the object of the class, right? So, it will skip this and move forward. It will see, okay, I have created an object of this upper class. So, local equals to new local. Now, in my heap memory, a class named local will be created. Its reference will be local. Now, as soon as I do local.sayhello, my simply method will be called. Now, you think what will happen in memory. First of all, as soon as this line is executed, a local object is created in my heap memory. Then, as soon as I write local.sayhello, I put a dot operator on the local reference pointer, i.e. I entered the object and called the method of sayHello. Now the method of sayHello is declared here, i.e. Hello is simply printed on my screen. Now as soon as Hello is printed from the screen, I got out of this method. Now as soon as I got out of this method, where would I have gone? who called this method, now this method was called by this line. So I came here, came out of this, this method is also over. After this, where will I go back? Who called this method, this method was called by this line. Now I came out of this, and finally this method is also over. Right, so flow of control works like this. If I run it now, then I will have hello print. And this was a way to make a local class. Now you will ask, what is the benefit of local class? Why have I made a local class here? Its sense depends on the business logic. Local class means that the class is linked to a method. For example, I made a class in a method. We will make this class only when its use is only in this method. Because as soon as this method ends, there will be no use of this class. If we want to make a class, Which will logically group some methods, Within another method, Whose use is basically within that method, We make a local class there. Again, I had told you earlier, There are very few use cases, You might see very few in production, But because it exists in Java, You should know that you can do that. Now again, You said that, There can be a particular use case for this, Yes, let's say, There is say hello here, Again, there is another method, Say, Bye, Or not say bye, Let's say, Say, Hi, Okay, One is above, Say hello, Okay, If you want, you can make it more functional. Let's say, say, take care, whatever. So you made a lot of different methods. And this greet can call any of these methods, whether it is sayHello, sayHi, sayTakeCare. First of all, you made a local object. Then from this local, whether you call sayHello, sayHi, sayTakeCare, it will be printed on the screen. So you can do this, take a parameter in the greet, what you want to call, and according to that parameter, I will make an object of any one and simply call it. Again, you can simply replace it with an if-else. Generally, all the local classes that we make can be replaced by some other code to a great extent. You won't need to make a lot of local classes. There might be a use case for some of them. If you see any benefit in making a local class, if your code looks better than that, then you will make a local class. Otherwise, it has a very rare use case. Now let's see which code blocks you can make this local class in. So basically in any code block, whether you make it in a constructor, whether you make it in a method, whether you make it in an if statement, whether you make it in a loop, you can make it in any of them. In fact, you can also make a class in a static block. You saw that we make a static block, you can also make a local class in it. Now let's talk about what a local class can access. Like we talked about static nested class and inner class. Logically, this is a non-static class. So every rule will be applied on it which was applied on our inner class. This means that it can also use our outer class static methods or non-static methods, static variables or non-static variables. It can easily call everyone inside. There is no problem. But there is a very important rule in this which we call effectively Effectively final variable But in this local class There is a very strict rule Which we call Effective final variable rule Okay Now what is that rule Let's understand that once It is very interesting And it will also sound very logical to you So I will do one thing I will take this example I will remove all this Okay So Let's say I made an outer class with the method greet and a local class with the method sayhello. What I do outside this class is that I create the local object and call local.sayhello. Okay. Let's say there are some variables in this class outer. Let's say a private int x which is equal to 4. So could my local class use this? Could I print x instead of hello? I could have done it, there was no error If I want to see it on the screen, then it should be 4 print No problem at all If I had this static If I write static private int x equal to 4, then also I could access it. If I run it now, then it should be 4printed, no problem. Right? So again, the same rule which was used in our inner class, whether it is static or non-static, we can access all the members. Obviously, if it is private, then we can also access the private. But look, this int x variable used to come in the outer class. Means it was an instance variable, right? What if I declare a variable in this greet method, int y. Now my variable y is coming in this greet method, which means that this is also a local variable. Now can this local class access this local variable? Let's say I gave a value of 5. Now if I print y instead of x, then is my print happening? Yes, it is happening. Now if I run it, then my output should be 5. It is coming. But now look carefully. Let's remove this first. I have created an outer class with the method greet, in which I have created a local variable y equal to 5. And there is a local class in it, which is trying to access this y. So everything is working fine till now. And here I am calling the outer.greet. But what if I do y + + in the next line? As soon as I did it, I got an error here. What does the error say? Local variable y defined in an enclosing scope must be final or effectively final. That's what I was talking about, effective final variable. What happens is, Java says that the y that you declared here, local variable, this applies only to local variables. I can use it here, but only if it is final. And if it was final, I couldn't write this line because I can't change the final variable. And if you want me to not write final here, Even then, I should not write this line. Basically, I should not modify this y in any way. Or I should just declare it as final, that it cannot be modified. And even if I am not declaring it as final, then I should not modify it in my code. Meaning, it should be an effectively final variable. Effectively final means, even if you don't declare it as final, but you haven't done any work to modify it. So, in a way, it is effectively final. The end result is final. So the variable should be effectively final if you want to use it in a local class. Okay, this is very interesting. So basically, if I did y + +, it won't let me do it. But why is it like this? This is a very specific rule. Why is it like this? So let's go to the iPad and see the reason why it is like this. So look, I wrote the same code here. Whatever we were talking about earlier, we had an outer class. It had a method called greet, which had a local variable y equal to 10. In this method, we had a local class with the method fun and we were trying to print y on the screen Now after this class, I used to make an object of local, local l equal to new local and call l.fun which means it will call this method and it will print y on the screen So this was running, meaning on my screen, if this is my screen, then the output used to be 10, very good. But as soon as I used to write some y + + in front of this line, my compiler error used to come here. We need to understand about that. Okay. Think about why this is happening. How long is the scope of our local class? Since this local class comes under a create method, as soon as this method is over, the scope of our class is also over. Right? But the scope of the class does not mean that the meaning of this reference variable is also over. Okay, what does it mean? Now, understand this in a way that the meaning of local class is actually happening in heap memory. As soon as this greet method calls, we just called this greet from the main method in the code. As soon as this greet method calls, let's check what happens in the memory. So I made it a little smaller. Now assume this is your heap memory. I have made it a heap memory. Now imagine as soon as this method calls, what happens first? First of all, int y is equal to 10. This is a local variable. Obviously, you know where local variables go. They go in the stack. then int y equals to 10 will be in our stack. Very good. And do you know when its scope will be? Till the scope of this method remains. Ok, very good. After that, what will happen? Now forget this line y + +, we have declared a class directly. Okay? For now, our compiler will leave this class because until the object comes, it will not make that class. Now, as soon as we create an object of this, local l equal to new local. As soon as you write new keyword, do you know what happens? A block is allocated in our heap memory. Okay? Okay. So, this block is an object of our local class. Now, the reference of this local class, l, where will we store it? Again, our I will store it in the stack memory. So here I have put L somewhere which is pointing at this. So this is also in our stack memory. Now you think what I did in the end. I called L.function. As soon as I call L.function, this method will be called on my screen. Fun method will be called. Y will be printed on the screen. If I have not written this, if I do not get any error because of this, then I remove the error. Okay, so I removed the error from you. Because I agree that I haven't written this yet. So, my screen will be printed 10 quietly. No problem at all. But, as soon as I introduce this line, Y + +. So, what does Y + + mean? I am saying, first declare Y equal to 10. Then, make 10 equal to 11 in stack memory. How did the problem start here? The problem starts here. You think, Java says, that this method will be finished. As soon as you reach here and finish this line, the method will be finished. And as soon as this method is finished, from our stack memory, the scope of y equal to 10 is finished. And the scope of this L reference variable is finished. But till now, this object is created in memory. You might say that the garbage collection of Java removes this object. The answer is absolutely correct, it removes it. But it does it only when if nothing is pointing it. Suppose, in some way, in this greet method, before this method is finished, if I pass on this L reference to someone else, now assume that my code doesn't end here. I have more space. Something like this And I have some other methods other than this greet So let's say I made a method Void Fun Okay Obviously, this fun and that fun are very different. Okay, I have taken a method here named voidFun. What it does is it calls this greet method. And this greet method returns this local variable l before it finishes. It returns this. So, obviously, its return type will change. What will be its return type here? Its return type will be local. And what we do here is we return this local reference variable Let's take it in L2. Obviously, the name can also be L here because this L and this L are different but I made it L2. So, local L2 equal to greet. So, what will happen now? Now, from outside this outer class, suppose from the main class, I will call the function fun. So, what will it do if I call fun? It will call greet. Now, this method will go to greet. As it is being called, it will create an object inside the heap memory. Its reference will be assigned to L. A Y equal to 10B heap, sorry, all this will become stack memory. Now, as soon as this function finishes, it will return l. returned L, who did we return? whoever is here to correct L, L2 now where will this L2 be? it will be in our stack memory so L2 will be in the stack memory what did we get? what did the greet return? what did the greet return? a reference pointer so the greet returned us a reference pointer of this object as soon as the greet is finished so obviously this pointer is finished so this is finished right? similarly our L is also gone this Y equal to 10 is also gone this is all gone from the stack memory But stack memory has L2, which is still pointing to the local class object whose function is already finished. This can happen. There are some changes in this, which I will tell you later. So if this can happen, then the object of the local class will outlive its parent function. The one who declared it, in which it was declared. You had a function called greet, in which I made a local class. Although my function called greet is finished, but the object of this local class still exists in memory. Because such scenario can happen, so what Java says is that whenever you try to access a local variable, like I tried to access y here, then I will copy this y equal to 10 in this class too. I will do it by myself, you don't need to write code, so what will the compiler do by itself? It will add a line of code here, int y equal to 10. He will add it by himself. He just copied this int y equal to 10 here. Okay, very good. He copied it because even if this method gets finished tomorrow, right? Even if this method gets finished tomorrow, and some other reference L2 is pointing to this object, and then someone tries to call L2. fun tomorrow, then also 10 should be printed on the screen. 10 means that the value of y should be printed. Because y was a local variable and if you want y to be printed at any time then y should be in the stack. But because y was a local variable then as soon as the grid ends, y will end. So what did our compiler do? As soon as the local class was created, it already copied int y equal to 10 here. Now what happened after copying here? It became a variable inside the class. Now where is the variable inside the class? Near the object. So here y is equal to 10. It came inside here too. Now as soon as it came, whenever someone calls L2. fun, Fun will automatically print y on the screen, meaning this one will print y. Very good. Because now this y is an instance variable, not a local variable. So Java does this. Now again let's go back to our question that why is it effectively final? If I make it y + +, what will be the problem? The problem will be that when you wrote y equal to 10 here, then the compiler simply put y equal to 10 in the stack. So you wrote y equal to 10, the compiler put y equal to 10 in our stack space. Then you are saying make y + +, so here it will make it 11. Then If you are declaring a local class, then what did the compiler do? It copied int y equal to 10 here as well. So the ambiguity here is that if you can change this y, then which y should I copy? Because if you keep changing the copy every time, then will I keep copying a new result every time? Absolutely not. And if I keep the starting result that first y equal to 10, then I will keep 10. And then you did y + + many times, right? So this is also wrong. So again, this is an ambiguous thing that what should Java do? that if you start changing this y later, then the y which it has copied inside, which y should it copy? So, if this ambiguity doesn't come, then Java won't let you do this. So, Java will make a strong rule of effective final. Okay? So this means that Java as a language is a very strict language. It doesn't allow any ambiguity, it makes a rule for it. So what does effective final say? You can't do this work. Either make it final, or I declare it final, or I don't do anything like this until my function is finished. If I don't change anything in it, then I can access it. If I change anything in it, then simply my error will come here. I have explained the reason for that error to you. Because the local class will get confused that which y should I copy here. Why does it copy? You also know that. Because the greet will be finished, but this object will remain in memory. How will it remain? It is possible that this method returns l and someone is sitting here to take the reference of l. Very good. All these are correct, but you must have one question in your mind. Is this line wrong? Which one? This line. There should be a compiler error in this. Because how did you declare the reference of local class here? Local is over here. As soon as its scope is over, you can't write local here. If you write local, there will be a red line. What is local? I don't even know this local class. Right? Absolutely correct. If you write it in code, then it won't be possible. What I have explained to you, right? It won't be possible. You will return L, but you won't be able to write return type local here. Because to have return type local, it should know what local is. Now local is declared inside a function. Where is it? Yes, here is local. Inside this grid, right? So the access of this local is inside this grid. Outside this, there is no access of this class. So neither I will be able to write local here, there will be an error. And neither will I be able to write local here, there will be an error. So this scenario can never happen, it can happen. If I return a special class here instead of local. The name of that special class is object class. In which O is capital, object. And here when I am taking it, I am granting value in it. And here also I take the value of an object named variable, let's say O. Something like this. Now it will work in the same scenario. Now the question is, what is an object? What kind of class is this? We haven't talked about it yet. We used to call every object that was made in heap memory. Is there any class for this? The answer is yes. In our Java, there exists a special class which we call object class. Its name is object class. We will study about this class in a separate video because it is a very important class and it has a lot of things which we will study separately. Apart from this, everything else in the local class is the same as in the inner class. So, whatever you can do in the inner class, you can do all those things in the local class too. Except, there was a strict rule that you cannot access the local variables unless and until it is not an effective final. Now, let's move towards our last class which is very interesting. We call it our anonymous class. Now you know what anonymous means. Anonymous means a class without a name. A class without a name is an anonymous class. Why would we make such a class that doesn't have a name? It's very interesting. Let's see it in code and you will enjoy it. So again, I have started my demo class. I will write here that we are going to understand our anonymous class. Suppose I have made a class whose name is let's say person. It is very simple. And there is only one method in this class. Introduce. This method just prints on the screen. System.out.println and it says hi I am a person. Now if I want to print this, I would have to make an object of it. So what I would do is, p1 equal to new person. I would make an object of it like this. Equal to new person. I would make an object of it like this. And here p1 dot, what would I call? Introduce. Okay. So I would call like this and on my screen, hi, I am a person would be called. Okay, hi, I am a person. Very good. Now I want this to be the use case. But there is a special use case. For which whenever I call introduce. So instead of hi I am a person. Hi I am a guest print. Hi I am a guest. And that is a special use case. Apart from that it will never be used. It will be used only once. So how can I do it? So you will say brother it is very simple. Let's do one more class. Guest. and extend it from person. We can do this. And here we can override our introduce method. You know we can do this. Child class can override its parent class method. So here we have overrided it. And here we have copied it. Hi, I am a person. Hi, I am a guest. Hi, I am a Guest print. Okay. Very good. Okay. So now if I want to print this Hi, I'm a guest. So I won't do anything. I will make a new object of a person. P2 equal to new. This time I will make a new guest here. We have also read that a parent class reference can point to a child class object. Now whenever I will write p2. introduce dot introduce Okay. So in this case what will happen? I will comment this first. Okay. So in this case My hi, I'm a guest should be called. So let's see that too. Hi, I'm a guest. Absolutely right. But think what I told you. This use case of mine will come only once in the entire code. After that, it will never come. Every time, hi, I'm a person should be there. For any one use case, hi, I'm a guest should be printed. But even then, what did we have to do? We had to make a whole new class. We had to extend it from person to person. We overrided its method and then wrote this. After doing all this, this method of mine, our call, worked. This is where our anonymous class comes in. If you want to do something that you have to do only once, you will not need to do it again and again. That is, you have to make a class that you will only need to read once. So don't write such a big code for that. Java has given a shortcut, which we call anonymous class. Make a class that has no name. How? Instead of this code, I can write something like this. So I can write directly like this. That I write person p2. P2' s reference object. Equal to new person. New person. But if I do like this. And I do p2. introduce. And if I write p2. introduce. INTRODUCE So, here, it should be HI, I AM A PERSON CALL Right? Okay? Yes, HI, I AM A PERSON CALL Okay? Okay, but this is wrong We want HI, I AM A GUEST CALL For that, I won't finish the code on semi-colon I'll give a curly bracket here Something like this Now whatever I will write in this curly bracket, this is my complete anonymous class. So now understand that my class has started here. Now in this class, I can do everything that I did in the guest class. That means I can write this code here too. So what should I do? Basically, I should override a method. The name of the method is void introduce. Okay. And what should I print instead of that? Hi, I am a guest. Let's say this. So I will write it here. Hi, I'm a guest. I can do it like this. And now when I call p2. introduce, then hi, I'm a guest will be called. Let's run it first and then see how it works. Hi, I'm a guest. So this is a very special syntax that Java gives us. It says, how do you write? Person p2 equal to new person. And you put a semicolon here. But don't put a semicolon. Start a code block here. And as soon as you start a code block, understand that you have started an anonymous class. Anonymous class means a class that has no name. Look, we haven't written its name here. We have started that class. So, you equate it with this block. So, the block we have written here, this block is doing this work here. So, you can do everything in it that you did in this. You can override a method. So, here also we have overrided the introduce method. After overriding, we had to write hi, I am a guest. We wrote it here. So, I didn't have to make a class in a separate file. I can remove this. Let me comment on this. I didn't have to do this. I used it directly and wrote Hi, I am a guest. Now, obviously, its best use case is that I had to do this only once. If I had to use Hi, I am a guest again and again, then I would have to make anonymous classes every time. I would have done this better. So, obviously, I will make it in anonymous class only when I want this particular line in a special case. So, there will be a special override. In that case, I made it anonymous. In the rest of the case, I will keep running my normal person class. This class. Okay? Okay. So, guys, you understood. Anonymous class is very interesting. But it will have some properties. Yes, it also acts like a normal class. But it has some rules. Like, look. In this, there can be some variables. Let's say I can put a string here. Name. And I can put a value here. Let's say Aditya. This is allowed. There is no problem. I can do this name. And if I want, instead of Hi, I am, I can print the name here. Something like this. So all this is allowed. What I do in the normal class is that I can declare some variables. In fact, apart from the variables, I can also declare some methods. Let's say void, greet. Let's say system.out.println just says hello. Something like this. So I can do all this. So I can do everything in normal class except one rule that I can't make a constructor in it. Let's see why I can't make a constructor in it. But one more thing. If I can write such a greet method. So I have introduced p2. Here it should be called hi, I am Aditya. So let's see if this call is happening. Yes. Hi, I am Aditya. So, I am getting a call. No problem. But can I call P2' s greet? Greet. As I wrote, there is an error here. What is the error? This method, greet is undefined for type person. I cannot do it. Why? Think. You think, this P2, which reference variable is this? This is a person type reference variable. And in our original person class, there was no method called greet. So, I cannot call greet on this P2. Right? This is basic inheritance and polymorphism. I can call greet on this P2 only if greet exists in my person. So I can't call this greet. So if I can't call this, then why can't I write in anonymous class? So Java says that you can write new methods in anonymous class, but you can't call them directly like this. So what's the benefit? The benefit is that the method I'm actually overriding, like I'm overriding introduce because introduce exists in person, I can call greet in it if I want. So suppose I called greet here, then first hello will come. And then it will print, Hi, I am Aditya. But here I can only call p2. introduce. So now both my things will be printed. Hello, hi, I am Aditya. See? So basically, I can make more methods just for this. So that my main method, which I am overriding, like here, introduce, I can call those methods in it. So in this introduce, I can call greet, I can call 4-5 more things, or I can use name variable here, I can do all that. But I can't call introduce from this p2. Or see, I can't even access name from p2. I can't even write this p2. name. Even if I do system.out.println p2. name, that is also wrong. Why? Because see, again, the answer is the same. Because p2 is a person type reference variable and this person doesn't have a name field or a greet method. So whatever is not in it, I can't do it with p2. p2. introduce is the only work that I can do. So in the anonymous class, we understood one thing that we can write other types of methods, declare variables, but I can't call them directly. There is only one way that I can use it in the one I am overriding. Now the second thing, I said that there can't be constructors in an anonymous class. Why can't there be? Think about it. What is a constructor? It helps to create an object, right? And we said that there is a rule of constructor in Java that its name should be exactly the same as the name of our class. So if I want to make a constructor of this person, then it will become something like this, in the name of person. So whether I make it default or parameterized, but its name should be person only. Now, my anonymous class doesn't have a name of its own. There is no name here. So, if there is no name of its own, how can I make it a constructor? So, I can't make any constructor of anonymous class. Okay? I hope you have understood why anonymous class will be used. Obviously, we have told that if there is only one particular use case where you have to do one thing and you don't want to do it again, In that case, you don't need to create a new class. You can simply do that work by creating an anonymous class. Basically, you can create a short or localized work by creating an anonymous class. You don't want to write the entire code in a separate file again and again. But if you know that the work is going to happen again and again, then the anonymous class is a waste because you will create it again and again. What you should do is to create a separate class for it. Now, what are the rules of this class? I have made this parent class inside the demo. So, it is also a nested class. Earlier, it was made separately here, which was far from this parent. But now, inside demo 4, this anonymous class exists. So, what are the rules of this? The rules of this are the same as the rules of our inner class. static and non-static variables of the demo4. The rules of the inner class are the same as the rules of the constructor class. Yes, the same rule is there in this too. If you have made this anonymous class in a method, then in that case, it becomes a local anonymous class, right? So, again, if it is a local class, then in that case, if you are using any local variable, then what should it be? It should be effective final. So, the rule that was applicable for local class, will be applicable for local anonymous too. Rest, it will behave like a dicto, like an inner class. The one I told you about, the constructor one, is different. Okay? So, this is all about the nested class. If I tell you its conclusion, Static nested class is used the most in our code base. So you should understand this well. Static nested class is used the most. After that, if we use any class, then that is our inner class, which is non-static nested class. This is also used less, but not so less. Many times you will see this inner class. Less than that is the use of our anonymous class. Anonymous class. Okay, this used to be used a lot in the old Java program. But now, it has been replaced by lambdas. We will study that later. What are lambdas? But instead of anonymous class, we use lambdas. So now, it has become rare. And the least used is local class. Very less, okay? You might see its use sometime. Or you might see its use sometime. But obviously, you need to know all the things. You need to know what kind of Java classes we can make. Nested classes. Because it becomes very important for interview point of view. Okay. So I hope you enjoyed it. You must have learned something new. Now please hype this video. And comment on it. Tell us if you learned something new. Or if you have any question, then ask that too. That's all in this video. See you in the next video. Till then, thank you so much.
