9.3 Inner class

Cliquez içi pour plus de vidéos disponibles sur notre chaine youtube !

Hello everyone, in this video we will talk about internal classes. An internal class is a class defined within another class called "external class" For example, it is like nested loops. So here we can have classes within other classes and this is useful if an internal class must have a limited scope. One of the reasons we use internal classes is because it helps group classes that go together. And it is extremely useful for listeners and for managing specific events. We have a main class here We will also create a class So, I will click on my source folder And then new class and call this class “outside”, then in our class “outside”, we will create another class and we will call it “interior”. So the inner class is going to be inside the outer class. Now, let’s assign some variables, In the outer class we will create a string character String x = Hello. And we will also create a variable in our inner class so it will be a String y = World. Now back to our main class to instantiate our classes. Exterior We’re going to call it this ext instance that equals new Exterior So, we have an instance of our class "Exterior" And we’re also going to instantiate our class "Interior" And to do that, we first have to type in the name of the outer class that acts as a container. So, we’ll type "Outside" dot the class name "Inside". We will call this instance in equals and then type the name of the instance of the external class So, ext.New Interieur Now we will display our variables x and y. And if we want to access X. then we have to type the name of the instance of the class "Exterior" ext point x And for the inner class it will be in. Now if we were to run this it shows Hello world. Now I could access a method from the "Inside" class so let’s create a method that will be "public void Salutation". What we are going to do here And go into our class hand to take our system . out and move it in our method Just x and y This time we have to call our method which is in the class "Inside" So, in.salutation Now, if we were to execute our code Now we can access a method which inside our class "Inside" was all for that video. In the next video, we will talk about the anonymous class that is the continuation of the internal class Go I tell you to the next.