image
Nested Classes
Start course
Difficulty
Beginner
Duration
1h 27m
Students
67
Ratings
5/5
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.

Learning Objectives

  • Object-oriented programming in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello there, my friends. In this video, we will discuss the nested class in relation to the use of the static keyword. In Java, we may define a class within another class. This class is known as the nested class. There are two types of nested classes in Java. The first one is the non-static nested class. This class is also known as the inner class. The second one is the static nested class. Non-static nested classes or inner classes may access all members such as static and non-static variables and methods of its outer class. Also an inner class may access private variables and methods of its outer class. On the other hand, static nested classes cannot access non-static methods or variables in the outer class. They can only access static variables and methods. The access modifier is not important here. The situation does not change even if the properties have the public access modifier.

So, let's go through an example to help you understand a little better. In the object oriented programming project, right click on the source folder and select new and class. I will specify the package name as the nestedclass and the class name as StaticNestedExample. Let's start to write a program that accesses static variables from nested class. Let's declare a static int variable a and its initial value be 5. Let's declare a private static int variable b and its initial value be 10. Lastly, let's declare a non-static variable int c and its initial value be 15. Okay, let's try to access these variables from static nested class. We need to create a static nested class in this class. Let's create a static nested class named StaticNestedClass, static class, StaticNestedClass. In this class, I will create a void method named show, void show(). In this method, I will use three print methods. System.out.println("a="+a); As you can see, in the first print method, we can access the static variable a.

Let's create the second print method. System.out.println("b=" + b); In the second print method, we can also access the private static variable b. And let's create the third print method. System.out.println("c="+ c); As you can see in the last print method, we cannot access non-private static variable c. This statement gives a compilation error because a static nested class cannot directly access the non-static variables, so let's put a comment on this line. Okay, let's create a new class for the test of StaticNestedClass. I will right click on the nestedclass package and select new and class. I will specify the class name as StaticNestedTest and select the checkbox to add the main method. Okay, now I want to access the show method in the StaticNestedClass. Since this method is not static, to reach this method, we need to create an object from the inner static class. In order to access the inner StaticNestedClass, we first use the outer class and then use the dot operator and nested class.

You got it, outer class first, then inner class. So, I write StaticNestedExample after the dot, I write StaticNestedClass. The outer class is StaticNestedExample and the inner class is StaticNestedClass. And if the show method in the inner StaticNestedClass was static, at this stage we could add one more dot and access the static method directly. We explained this in detail in the previous lesson. But in this example, since we do not define the show method as static, we must create a new object here and access the show method with this object. Now let's create a nested object by using the new keyword, nested = new StaticNestedExample.StaticNestedClass(); Also, I want to show you something else. You can remove the outer class if you don't use it in this manner.

This time, however, you should import the StaticNestedClass. Yeah, you heard right, you can import it. If you remember, we said that we perform the import process when using classes in different packages. But this way, we can still use the import operation when working with nested classes. For example, I'll copy and paste this line and turn the previous line into a comment line because I want this usage pattern to stay here as well. Now let's delete the outer class from here and here. As you can see, we are getting a compiler error. If we move the mouse over the StaticNestedClass and select the import StaticNestedClass, we have done the import process and we no longer receive errors from the compiler, but both usages are valid. Either import and do not use the outer class or create an object by writing the outer class and then the inner class before dealing with the import. So, now we can call the show method using the nested object. The show method will print the values of variables a and b. Okay, let's run the code.

You see the values of variables a = 5 and b = 10. Okay, now let's make an example with a non-static nested class i.e. inner class. Let's create a new class. I will right click on the nestedclass package and select new and class. I will specify the class name as InnerClassExample. Let's copy the declaration of three variables from the StaticNestedExample class. Okay, let's try to access these variables from inner class. We need to create an inner class in this class. Let's create the inner class named InnerClass. In this class, we need to create a void method show again. We use three print methods. In the first print method, we can access static variable a. In the second print method, we can access private static variable b. In the last print method, we can access non-private static variable c. That is an important point. In the StaticNestedClass, we couldn't access variable c but we can access it in the non-static inner class. In other words, if the inner class is static, other variables must be static to use them.

Okay, so let's create a new class for the test of inner class. I will right click on the NestedClass package and select new class. I will specify the class name as InnerClassTest and select the checkbox to add the main method. In order to instantiate an inner class, we must first instantiate the outer class, then create the inner object within the outer object. Let's create an outer object by using the new keyword first and then create an inner object. So, I write InnerClassExample outer = new InnerClassExample(); Now let's create an object from the inner class, InnerClassExample.InnerClass inner = outer.new InnerClass(); So, this syntax is a little bit different from the other. To create an object from the non-static inner class, you need to write it like that. Now let's delete the outer class from here. So, now let's move the mouse over the InnerClass and select the import InnerClass option.

As you can see, the compilation error has disappeared but you have to use this outer object here. If we remove this outer object here, we get a compilation error like that, no enclosing instance of type InnerClassExample is accessible. Must qualify the allocation with an enclosing instance of type InnerClassExample i.e. x.new a where x is an instance of InnerClassExample. So, just to be clear to create an object from the InnerClass, you should use this syntax, okay? So, now let's call the show method using the inner object. Now let's run the code and see the result. You see the values of variables, a = 5, b = 10 and c = 15. Great, so I think you understand the static and non-static inner classes now. Let's take a short break here my friends and I will see you in the next video.

 

About the Author
Students
3995
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.